diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..bc9ca10f 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -9,12 +9,13 @@ For each example, can you explain why we are seeing undefined? */ -// Example 1 +// Example 1 // console.log is returning undefined due to the "a" variable has not been assigned a value + let a; console.log(a); -// Example 2 +// Example 2 // console.log is returning undefined as "hello" has been assigned to the function sayHello() which does not have a return value function sayHello() { let message = "Hello"; } @@ -23,7 +24,8 @@ let hello = sayHello(); console.log(hello); -// Example 3 +// Example 3 //console.log is returning undefined as the parameter "user" has not been assigned a value + function sayHelloToUser(user) { console.log(`Hello ${user}`); } @@ -31,6 +33,8 @@ function sayHelloToUser(user) { sayHelloToUser(); -// Example 4 +// Example 4 // console.log is returning undefined as in JavaScript, arrays are zero-indexed with the first element of an array being 0, +// the second element being 1 etc. In this example, there are three elements, however, the index ends at 2. Therefore, arr[3] returns +// undefined as the index ends at 2. let arr = [1,2,3]; console.log(arr[3]); diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..e1c86ff4 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,7 +6,16 @@ */ function evenNumbers(n) { - // TODO + let evenArr = []; + let index = 0; + while (n > evenArr.length) { + if (index % 2 === 0) { + evenArr.push(index); + } + index ++; + //console.log(evenArr); + } + console.log(evenArr); } 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..d49bb3b2 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,14 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let index = 0; + + while (index < birthdays.length) { + if (birthdays[index].includes("July")) { + return birthdays[index]; + } + index ++; + } } 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..0da3cde3 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,14 @@ */ function evenNumbersSum(n) { - // TODO + let sum = 0; + let index = 0; + + do { + sum += index * 2; + index ++; + } while (index < 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..3aee25aa 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,7 @@ // Change the below code to use a for loop instead of a while loop. -let i = 0; -while(i < 26) { - console.log(String.fromCharCode(97 + i)); - i++; +for (let i = 0; i <26; i++) { + console.log(String.fromCharCode(97 + 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..dc4cc9ae 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -26,7 +26,9 @@ const AGES = [ 49 ]; -// TODO - Write for loop code here +for (let index = 0; index < WRITERS.length; index ++) { + console.log(`${WRITERS[index]} is ${AGES[index]} 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..98dbe375 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,12 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (let busStop of tubeStations) { + console.log(busStop); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (let char of str) { + console.log(char.toUpperCase()); +} \ No newline at end of file diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..2b28c708 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,12 @@ */ function getTemperatureReport(cities) { - // TODO + let arr = []; + for (const element of cities) { + if (temperatureService(element)) { + arr.push(`The temperature in ${element} is ${temperatureService(element)} degrees`); + } + } return arr; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..9f0e5a4a 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,7 +10,17 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let index = 0; + let arr = []; + do { + arr.push(generateRandomNumber()); + if (arr[index] > 50) { + arr = arr[index]; + } + index ++; + } + while (index <= arr.length); + return arr } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..9757e9f1 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,8 +5,12 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO -} + let arr = []; + for (const element of allArticleTitles) { + if (element.length <= 65) { + arr.push(element); + } + }return arr; /* The editor of the FT likes short headlines with only a few words! @@ -14,7 +18,12 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + const sortedArr = allArticleTitles.sort((a,b) => { + // console.log(a.split(" ").length); + return a.split(" ").length - b.split(" ").length; + }); + return sortedArr[0]; +} } /* @@ -23,7 +32,12 @@ 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 array = [] + for (const element of allArticleTitles){ + if ( /\d/.test(element)) { + array.push(element); + } + } return array; } /* @@ -31,8 +45,17 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO -} + let arrNumOfCharacters = []; + let total = 0; + let average = 0; + for (const element of allArticleTitles) { + arrNumOfCharacters.push(element.trim().length); + } + for (const element of arrNumOfCharacters) { + total = total + element; + } + average = total / arrNumOfCharacters.length; + return Math.round(average); diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..85fa1d9c 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,9 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + const average = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS.map(prices => prices.reduce((a, b) => a + b) / prices.length); + // console.log(average) + return average.map(value => Number(value.toFixed(2))) } /* @@ -48,7 +50,8 @@ function getAveragePrices(closingPricesForAllStocks) { The price change value should be rounded to 2 decimal places, and should be a number (not a string) */ function getPriceChanges(closingPricesForAllStocks) { - // TODO + const priceChange = closingPricesForAllStocks.map(prices => prices[prices.length - 1] - prices[0]); + return priceChange.map((value) => Number(value.toFixed(2))); } /* @@ -64,7 +67,10 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + const sortedArr = closingPricesForAllStocks.map(prices => prices.sort((a, b) => b - a)); + return sortedArr.map((prices, index) => { + return `The highest price of ${STOCKS[index].toUpperCase()} in the last 5 days was ${prices[0].toFixed(2)}`; + }) } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..88cd69cb 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -8,9 +8,12 @@ Using a loop, complete the function below so it returns the factorial of the number being passed in. */ -function factorial(input) { - // TODO -} +function factorial(sum) { + let count = 1; + for (let index = sum; index >= 1; index --) { + count *= index; + } + return count } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..33b3befd 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,7 +11,22 @@ */ function getHighestRatedInEachGenre(books) { - // TODO + let genreList = ["non-fiction", "children", "cooking"]; + let maxRatingTitles = []; + for (const genre of genreList) { + let maxRating = 0; + let maxIndex = 0; + for (let i = 0; i < books.length; i++) { + if (books[i].genre === genre) { + if (books[i].rating > maxRating) { + maxRating = books[i].rating; + maxIndex = i; + } + } + } + maxRatingTitles.push(books[maxIndex].title); + } + return maxRatingTitles; } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..f8f4812f 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,11 @@ */ function generateFibonacciSequence(n) { - // TODO + let fibSeq = [0, 1]; + for (let i = 0; n > fibSeq.length; i++) { + fibSeq.push(fibSeq[fibSeq.length - 1] + fibSeq[fibSeq.length - 2]); + } + return fibSeq; } /* ======= TESTS - DO NOT MODIFY ===== */