From 5500f8ac17acb65cfb7302ae43c1400ed50f9598 Mon Sep 17 00:00:00 2001 From: Abdirahim Hussein Date: Fri, 19 Aug 2022 16:15:39 +0100 Subject: [PATCH 1/6] completed 3 tasks --- 1-exercises/A-undefined/exercise.js | 6 ++++++ 1-exercises/B-while-loop/exercise.js | 8 ++++++++ 1-exercises/C-while-loop-with-array/exercise.js | 9 +++++++++ 3 files changed, 23 insertions(+) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..5e6af88a 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,6 +12,7 @@ // Example 1 let a; console.log(a); +// the reason why we are seeing undefined for a has no value assigned // Example 2 @@ -21,12 +22,15 @@ function sayHello() { let hello = sayHello(); console.log(hello); +// the function is undefined because it has not returned anything + // Example 3 function sayHelloToUser(user) { console.log(`Hello ${user}`); } +// the user parameter does not pass anything sayHelloToUser(); @@ -34,3 +38,5 @@ sayHelloToUser(); // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// there is no index 3 in the array the index goes up to 2 + diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..f7d7e86b 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,6 +7,14 @@ function evenNumbers(n) { // TODO + const arr = [] + let i = 0 + + while (i < n) { + arr.push(i * 2); + i++; + } + console.log(arr.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..9d08be2f 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,6 +17,15 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { + let i = 0; + + while (i < birthdays.length - 1) { + let birthdayCheck = birthdays[i]; + if (birthdayCheck.startsWith("July")) { + return birthdayCheck; + } + i++; + } // TODO } From 8937ccd3e41e22c3ba8667ede6fbd01bbd8e1ffc Mon Sep 17 00:00:00 2001 From: Abdirahim Hussein Date: Fri, 19 Aug 2022 16:47:14 +0100 Subject: [PATCH 2/6] i have completed 5 other exercises --- 1-exercises/D-do-while/exercise.js | 11 +++++++++++ 1-exercises/E-for-loop/exercise1.js | 3 ++- 1-exercises/E-for-loop/exercise2.js | 3 +++ 1-exercises/F-for-of-loop/exercise.js | 10 ++++++++++ 2-mandatory/1-weather-report.js | 6 ++++++ 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..d6244f88 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,17 @@ function evenNumbersSum(n) { // TODO + if (n == 0) return 0 + let i = 0 + j = 1 + sum = 0 +do { + i += 2 + sum += i + j++ +} +while (j < 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..26a28f70 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -8,7 +8,8 @@ // 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++; -} +}} // 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..0561c380 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,9 @@ const AGES = [ ]; // TODO - Write for loop code here +for (let i = 0; i Date: Fri, 19 Aug 2022 17:11:47 +0100 Subject: [PATCH 3/6] completed the mandotary exercises --- 2-mandatory/2-retrying-random-numbers.js | 6 +++++- 2-mandatory/3-financial-times.js | 19 +++++++++++++++++++ 2-mandatory/4-stocks.js | 8 ++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..c9f21eb2 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 -} + i = 0; + do { + i += generateRandomNumber(); + } while (i < 50); + return i;} /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..f4e8ccd7 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,6 +6,8 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + const article = allArticleTitles.filter((article) => article.length <= 65); + return article; } /* @@ -15,8 +17,17 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + const outputOfArticle = allArticleTitles[0]; + for (let i = 0; i < allArticleTitles.length; i++) { + if ( + allArticleTitles[i].split(" ").length < outputOfArticle.split(" ").length + ) + outputOfArticle = allArticleTitles[i]; + } + return outputOfArticle; } + /* The editor of the FT has realised 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. @@ -24,6 +35,8 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + const title = allArticleTitles.filter((article) => article.match(/(\d+)/)); + return title } /* @@ -32,6 +45,12 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + const total = 0; + for (let title of allArticleTitles) { + total += title.length; + } + let average = total / allArticleTitles.length; + return Math.round(average); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..ef86a172 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -35,6 +35,11 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + return closingPricesForAllStocks.map((priceArr) => { + return Number( + (priceArr.reduce((tot, num) => tot + num) / priceArr.length).toFixed(2) + ); + }); } /* @@ -49,6 +54,9 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + return closingPricesForAllStocks.map((priceArr) => { + return Number((priceArr[priceArr.length - 1] - priceArr[0]).toFixed(2)); + }); } /* From 33f749c1a1776ff61276816c6d779ce43e4d67b7 Mon Sep 17 00:00:00 2001 From: Abdirahim Hussein Date: Tue, 6 Sep 2022 16:57:36 +0100 Subject: [PATCH 4/6] made a few changes --- 1-exercises/E-for-loop/exercise1.js | 5 +---- 2-mandatory/1-weather-report.js | 12 +++++------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index 26a28f70..1f81ac9d 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,10 +6,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++; -}} +} // The output shouldn't change. diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index 1fb85ab2..e5d1c1ff 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,14 +13,12 @@ function getTemperatureReport(cities) { // TODO - let temperature = []; - for (city of cities) { - let temp = temperatureService(city); - temperature.push("The temperature in", city, " is ", temp, " degrees"); - } - return temperature; +let temperature = []; +for (let i = 0; i < cities.length; i++) { + tem.push(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`); +} +return temperature; } - /* ======= TESTS - DO NOT MODIFY ===== */ From 20044725a8449b70aace614afa1fccd2603256a0 Mon Sep 17 00:00:00 2001 From: Abdirahim Hussein Date: Tue, 6 Sep 2022 17:17:25 +0100 Subject: [PATCH 5/6] reevaluated the last challenge --- 2-mandatory/4-stocks.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index ef86a172..73465f46 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -73,6 +73,13 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + let highestPriceDescriptions = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + highestPriceDescriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[i] + ).toFixed(2)}` + ); + } + return highestPriceDescriptions; } From ddb1f63eb2892859ad4542c31d4a8f269205d818 Mon Sep 17 00:00:00 2001 From: Abdirahim Hussein Date: Tue, 1 Nov 2022 22:34:37 +0000 Subject: [PATCH 6/6] code indented --- 1-exercises/D-do-while/exercise.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index d6244f88..a5371bcf 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -12,13 +12,13 @@ function evenNumbersSum(n) { let i = 0 j = 1 sum = 0 -do { - i += 2 - sum += i - j++ -} -while (j < n) -return sum + do { + i += 2 + sum += i + j++ + } + while (j < n) + return sum } console.log(evenNumbersSum(3)); // should output 6