diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..f82af5a7 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,17 +11,19 @@ // Example 1 let a; -console.log(a); +console.log(a); +// there's no values assigned to the variable. // Example 2 function sayHello() { let message = "Hello"; } +// there's no output. let hello = sayHello(); console.log(hello); - +// because the function sayhello does not return anything so this is not defined. // Example 3 function sayHelloToUser(user) { @@ -29,8 +31,9 @@ function sayHelloToUser(user) { } sayHelloToUser(); - +// no argument. // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// there's no 4th(arr index 3) element in the arr. \ No newline at end of file diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..950b9058 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,7 +6,13 @@ */ function evenNumbers(n) { - // TODO + let result = []; + let i = 0; + while(i < n) { + result.push(i * 2); + i++; + } + console.log(result.join()); } 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..466805b3 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,13 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let i = 0; + while (i < BIRTHDAYS.length) { + if (BIRTHDAYS[i].startsWith("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..9faf799b 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,16 @@ */ function evenNumbersSum(n) { - // TODO + let sum = 0; + let i = 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..7731a700 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -7,8 +7,7 @@ // 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..5f3f3eb6 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,7 +27,9 @@ const AGES = [ ]; // TODO - Write for loop code here - +for ( let i = 0; i < WRITERS.length; 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..827eee27 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -1,5 +1,6 @@ /* - A for-of loop is a easy and way of looping through the elements of an array, string or any other "iterable object" (think sequence of elements). + A for-of loop is a easy and way of looping through the elements of an array, + string or any other "iterable object" (think sequence of elements). */ // TODO Use a for-of loop to output each of the tube stations below. @@ -10,7 +11,12 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; - +for (let station of tubeStations) { + console.log(station); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (let 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..92694bbd 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,10 +12,17 @@ */ function getTemperatureReport(cities) { - // TODO + let report = []; + for (let city of cities) { + let temparature = temperatureService(city); + report.push(`The temperature in ${city} is ${temparature} degrees`); + + } + return report; } + /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..6e09cfa7 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,7 +10,12 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let generatedNumber; + do { + generatedNumber = generateRandomNumber(); + + } while (generatedNumber <= 50); + return generatedNumber; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..85cc7842 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,7 +5,13 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + let headlines = []; + for (let title of allArticleTitles) { + if (title.length <= 65) + headlines.push(title) + + } + return headlines; } /* @@ -23,7 +29,14 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + let articleWithNumbers = []; + + for ( let title of allArticleTitles) { + if (character >= "0" && character <= "9") + articleWithNumbers.push(title) + } + + return articleWithNumbers; } /* @@ -31,7 +44,12 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let totalCharacters = 0; + + for (let title of allArticleTitles) { + totalCharacters += title.length; + } + return Math.round(totalCharacters / allArticleTitles.length); } diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..40f066ae 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -10,8 +10,23 @@ Each title in the resulting array should be the highest rated book in its genre. */ +// From the solution : function getHighestRatedInEachGenre(books) { - // TODO + // this will be an object where the property will be a genre, and the value will be an object representing the book + let highestRated = {}; + + for(let book of books) { + // if this is the first time we're seeing this genre OR the rating we've seen is not as high as the current book + if(highestRated[book.genre] === undefined || highestRated[book.genre].rating < book.rating) { + // then this book is now the highest rated in the genre so far + highestRated[book.genre] = book; + } + } + + // Here we just want to get the highest rated books (the values of the object) + // and then get the title for each one + return Object.values(highestRated) + .map(book => book.title); }