diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..7dbcdb49 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,15 +12,17 @@ // Example 1 let a; console.log(a); - +// here a hasn't assigned to any value. // Example 2 function sayHello() { - let message = "Hello"; + let message = "Hello"; + // this function doesn't return any value. } + let hello = sayHello(); -console.log(hello); +console.log(hello); // Example 3 @@ -29,8 +31,10 @@ function sayHelloToUser(user) { } sayHelloToUser(); - +// user hasn't been defined here when we called the function // Example 4 let arr = [1,2,3]; console.log(arr[3]); + +// There are only 3 parameters in this array so there isn't any index number of 3, indexes start from 0 to one less than the array length which is two here diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..298040ea 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -1,12 +1,19 @@ /* while loops can be useful when you want to execute some code as long as some condition is true. - Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-seperated string. + Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-separated string. The list of numbers should start with 0. n is being passed in as a parameter. */ function evenNumbers(n) { // TODO + let array=[] + let i=0 + while ( i < n) { + array.push(i * 2) + i++ + } + console.log(array.toString()) } evenNumbers(3); // should output 0,2,4 diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..d0b15874 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,13 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + let i=0 + while ( i < BIRTHDAYS.length){ + if (BIRTHDAYS[i].includes("July")){ + return birthdays[i] + } + i++ + } } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..86f32a24 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,14 @@ function evenNumbersSum(n) { // TODO + let i=0 + let sum=0 + do { + sum+=(i*2) + i++ + } + while (i < n) + return sum } console.log(evenNumbersSum(3)); // should output 6 diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..184a6789 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,9 @@ // Change the below code to use a for loop instead of a while loop. -let i = 0; -while(i < 26) { + +for (i = 0; i < 26; i++) { console.log(String.fromCharCode(97 + i)); - i++; + } // The output shouldn't change. diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..46ab408f 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -28,6 +28,10 @@ const AGES = [ // TODO - Write for loop code here +for ( i=0 ; i < 5 ; i++){ + console.log(`${WRITERS[i]} is ${AGES[i]} years old`) +} + /* The output should look something like this: diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..e2f8e4ab 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -10,7 +10,14 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; +for ( const element of tubeStations){ + console.log(element) +} -// TODO Use a for-of loop to capitalise and output each letter in the string seperately. +// TODO Use a for-of loop to capitalize and output each letter in the string separately. let str = "codeyourfuture"; + +for (const letter of str) { + console.log(letter.toUpperCase()) +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..25d56b42 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -2,7 +2,7 @@ Imagine we're making a weather app! We have a list of cities that the user wants to track. - We also already have a temperatureService function which will take a city as a parameter and return a temparature. + We also already have a temperatureService function which will take a city as a parameter and return a temperature. Implement the function below: - take the array of cities as a parameter @@ -13,6 +13,12 @@ function getTemperatureReport(cities) { // TODO + let tempRepo=[] + for (i=0 ; i < cities.length ; i++){ + let temp=temperatureService(cities[i]) + tempRepo.push(`The temperature in ${cities[i]} is ${temp} degrees`) + } +return tempRepo } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..5b009392 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,11 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let num; + do { + num = generateRandomNumber(); + } while (num <= 50) + return num; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..8e67f580 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,24 +6,62 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + let acceptedTitles=[] + let lengthCheck=0 + for ( i=0 ; i < allArticleTitles.length ; i++){ + lengthCheck= allArticleTitles[i].length + if (lengthCheck <= 65){ + acceptedTitles.push(allArticleTitles[i]) + } + } + return acceptedTitles } +// function potentialHeadlines(allArticleTitles){ +// let acceptedTitles=[] +// for (element of allArticleTitles){ +// ( element.length <= 65 )? acceptedTitles.push(element):next +// } +// return acceptedTitles +// } + /* The editor of the FT likes short headlines with only a few words! - Implement the function below, which returns the title with the fewest words. - (you can assume words will always be seperated by a space) + Implement the function below, which returns the title with the fewest words.e + (you can assume words will always be separated by a space) */ function titleWithFewestWords(allArticleTitles) { // TODO -} + let minValue + let indexOfShortestTitle + let wordCountOFTitles=[] + let i=0 + do { + let wordCount= allArticleTitles[i].trim().split(" ").length + wordCountOFTitles.push(wordCount) + i++ + } + while(i < allArticleTitles.length) + minValue= Math.min(...wordCountOFTitles) + indexOfShortestTitle=wordCountOFTitles.indexOf(minValue) + + return allArticleTitles[indexOfShortestTitle] +} /* - The editor of the FT has realised that headlines which have numbers in them get more clicks! + The editor of the FT has realized that headlines which have numbers in them get more clicks! Implement the function below to return a new array containing all the headlines which contain a number. (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { // TODO + let titleWithNum=[] + for (const title of allArticleTitles){ + if (/\d/.test(title)){ + titleWithNum.push(title) + } + } + return titleWithNum } /* @@ -32,6 +70,11 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + let sum=0 + for (i=0 ; i