From ed171c12a0ff5ae7333099f5efa7a67bcfa74be1 Mon Sep 17 00:00:00 2001 From: Emeka1993 Date: Fri, 19 Aug 2022 18:53:21 +0100 Subject: [PATCH] Commit --- 1-exercises/A-undefined/exercise.js | 4 +++ 1-exercises/B-while-loop/exercise.js | 13 ++++++-- .../C-while-loop-with-array/exercise.js | 31 +++++++++---------- 1-exercises/D-do-while/exercise.js | 9 ++---- 1-exercises/E-for-loop/exercise1.js | 1 + 1-exercises/E-for-loop/exercise2.js | 5 ++- 1-exercises/F-for-of-loop/exercise.js | 15 ++++++--- 2-mandatory/1-weather-report.js | 3 ++ 2-mandatory/2-retrying-random-numbers.js | 6 +++- 2-mandatory/3-financial-times.js | 22 ++++++++++++- 2-mandatory/4-stocks.js | 27 +++++++++++++++- 11 files changed, 103 insertions(+), 33 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..654f2a33 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,12 +12,16 @@ // Example 1 let a; console.log(a); +//the value is missing + // Example 2 function sayHello() { let message = "Hello"; } +//the function is returning nothing + let hello = sayHello(); console.log(hello); diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..915f69e6 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -4,10 +4,17 @@ Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-seperated string. The list of numbers should start with 0. n is being passed in as a parameter. */ - function evenNumbers(n) { - // TODO -} + let arrayOfEvenNumbers = []; + let i = 0; + while (n > arrayOfEvenNumbers.length) { + if (i % 2 === 0) { + arrayOfEvenNumbers.push(i); + } + i++; + } + console.log(arrayOfEvenNumbers); + } evenNumbers(3); // should output 0,2,4 evenNumbers(0); // should output nothing diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..1c3be372 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -3,21 +3,20 @@ In the below example, imagine we've defined an array holding the birthdays of your closest friends. Use a while loop to search through the array until you find the first birthday in July, then return that birthday from the function. */ - -const BIRTHDAYS = [ - "January 7th", - "February 12th", - "April 3rd", - "April 5th", - "May 3rd", - "July 11th", - "July 17th", - "September 28th", - "November 15th" -]; +while (condition) { + const BIRTHDAYS = [ + "January 7th", + "February 12th", + "April 3rd", + "April 5th", + "May 3rd", + "July 11th", + "July 17th", + "September 28th", + "November 15th" + ]; + + } function findFirstJulyBDay(birthdays) { - // TODO -} - -console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" + return (birthdays \ No newline at end of file diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..c814adda 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -6,10 +6,7 @@ Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0) */ -function evenNumbersSum(n) { - // TODO +for (let i = 0; i < 11; i++) { + console.log(i*9); } - -console.log(evenNumbersSum(3)); // should output 6 -console.log(evenNumbersSum(0)); // should output 0 -console.log(evenNumbersSum(10)); // should output 90 \ No newline at end of file + // should output 90 \ No newline at end of file diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..71411771 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -8,6 +8,7 @@ // Change the below code to use a for loop instead of a while loop. let i = 0; while(i < 26) { +for (let i = 0; i < 26; i++) { console.log(String.fromCharCode(97 + i)); i++; } diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..5a9e246c 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,7 +27,10 @@ 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..6dd218ca 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -4,13 +4,20 @@ // TODO Use a for-of loop to output each of the tube stations below. let tubeStations = [ - "Aldgate", - "Baker Street", + "Picadilly Circus", - "Oxford Street", - "Tottenham Court Road" + ]; +for (stations of tubeStations){ + console.log(stations) +} + + // TODO Use a for-of loop to capitalise and output each letter in the string seperately. + let str = "codeyourfuture"; +for (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..54ade8a3 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,9 +13,12 @@ function getTemperatureReport(cities) { // TODO + let cityTemperatures = cities.map(city => `The temperature in ${city} is ${temperatureService(city)} degrees`) +return cityTemperatures } + /* ======= 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..962a7dd3 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,7 +11,11 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop -} + do { + value = generateRandomNumber() + } while( value <= 50) + return value + } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..aeaa2e86 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,6 +5,9 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { + let selectedHeadings = allArticleTitles.filter(article => article.length <= 65); + return selectedHeadings; +} // TODO } @@ -14,7 +17,9 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let smallestArticle = allArticleTitles.reduce((initial, next) => (initial.length < next.length)? initial : next); + return smallestArticle; + } } /* @@ -24,6 +29,15 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + let selectedHeadings = allArticleTitles.filter((element) => { + for (let i = 0; i < element.length; i++) { + if (parseInt(element[i])) { + return true; + } + } + return false; + }); + return selectedHeadings; } /* @@ -33,6 +47,12 @@ function headlinesWithNumbers(allArticleTitles) { function averageNumberOfCharacters(allArticleTitles) { // TODO } +let totalCharacters = allArticleTitles.reduce((acc, curr) => acc + (curr.length), 0); + + let average = totalCharacters / (allArticleTitles.length) + + return Math.round(average); +} diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..f32255d0 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,12 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + const average = closingPricesForAllStocks.map((arr) => + arr.reduce((acc, curr) => acc + curr / arr.length, 0) + ); + const roundedStockPrices = average.map((element) => parseFloat(element.toFixed(2))); + return roundedStockPrices; + } /* @@ -49,6 +54,13 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + let changeInStocks = closingPricesForAllStocks.map( + (arr) => arr[arr.length - 1] - arr[0] + ); + + changeInStocks = changeInStocks.map((element) => parseFloat(element.toFixed(2))); + + return changeInStocks; } /* @@ -66,6 +78,19 @@ function getPriceChanges(closingPricesForAllStocks) { function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO } +let arrayOfHighestPrices = closingPricesForAllStocks.map((arr) => +arr.reduce((acc, curr) => { + return acc > curr ? acc : curr; +}) +); +let stocksDescription = []; +for (let i = 0; i< arrayOfHighestPrices.length; i++){ +stocksDescription.push( + `The highest price of ${(stocks[i]).toUpperCase()} in the last 5 days was ${(arrayOfHighestPrices[i]).toFixed(2)}` +); +} +return stocksDescription + /* ======= TESTS - DO NOT MODIFY ===== */