From 130f2e996383f29a4d3b440c3c3e701ceab56ce2 Mon Sep 17 00:00:00 2001 From: leilafaez <79403281+leilafaez@users.noreply.github.com> Date: Sun, 18 Sep 2022 23:15:35 +0100 Subject: [PATCH 1/3] Update exercise folder --- 1-exercises/A-undefined/exercise.js | 24 +++++++++++++++---- 1-exercises/B-while-loop/exercise.js | 12 +++++++++- .../C-while-loop-with-array/exercise.js | 6 ++++- 1-exercises/D-do-while/exercise.js | 11 ++++++++- 1-exercises/E-for-loop/exercise1.js | 11 +++++---- 1-exercises/E-for-loop/exercise2.js | 6 ++++- 1-exercises/F-for-of-loop/exercise.js | 7 +++++- 7 files changed, 64 insertions(+), 13 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..e44966f7 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,25 +12,41 @@ // Example 1 let a; console.log(a); +//here by console.log we can print a's value. (a) has declared but has not initialized so it will return undefined. +//we can change Example 1 like below : + a=10; +console.log(a); // Example 2 function sayHello() { let message = "Hello"; -} - +}//because this function doesn't return message so it shows undefined. let hello = sayHello(); +console.log(hello); // this will print a variable which is hold the value of sayHello funcation call but cuz the +//function doesn't return message , so it again shows undefined. but if the function fix and return message +//these lines will be show "Hello". we can fix it as bellow. + +function sayHello(){ + let message="Hello"; + return message; +} +hello=sayHello(); console.log(hello); + // Example 3 function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser();// in this example sayHelloToUser function has a parameter so because an argument doesn't pass to function call it will show "Hello undefined". +//we can fix it as bellow : +sayHelloToUser("Leila");//it will show "Hello Leila" // Example 4 let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]);//this array has 3 element and include : 0 1 2 indexes so index 3 doesnt exist in this array +//and will show undefined diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..d54f91f2 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,7 +6,17 @@ */ function evenNumbers(n) { - // TODO + let counter = 0; + let show = ""; + while (counter < n * 2) { + if (counter % 2 == 0) { + show = show + counter + ","; + } + + counter++ + } + console.log(show); + } 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..6dcb14b3 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,11 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let counter = 5; + while (counter < BIRTHDAYS.length) { + return birthdays[counter]; + } + counter++; } 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..1d019f6a 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 counter = 0; + let sum = 0; + do { + if (counter % 2 === 0) { + sum += counter; + } + counter++; + } while (counter < n * 2); + + console.log(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..fe9af130 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,12 @@ // 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++; +// 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..70a48205 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,7 +27,11 @@ const AGES = [ ]; // TODO - Write for loop code here - +for(let i=0;i Date: Tue, 20 Sep 2022 11:38:11 +0100 Subject: [PATCH 2/3] Update mandatory exercise --- 1-exercises/F-for-of-loop/exercise.js | 2 +- 2-mandatory/1-weather-report.js | 8 +++++++- 2-mandatory/2-retrying-random-numbers.js | 6 ++++++ 2-mandatory/3-financial-times.js | 24 ++++++++++++++++++++++++ 2-mandatory/4-stocks.js | 20 ++++++++++++++++++-- 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 12f8f844..fd5faf59 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -17,5 +17,5 @@ for (let item of tubeStations) { // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; for (let item of str) { - console.log(item); + console.log(item.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..67f89087 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,13 @@ */ function getTemperatureReport(cities) { - // TODO + let report = []; + + + for (let i = 0; i < cities.length; i++) { + report[i] = `The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`; + } + return report; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..742a76d0 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,12 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let counter = 0; + + do { + counter = generateRandomNumber(); + } while ( counter<=50); + return counter; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..8fa64ea1 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,6 +6,10 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + if (allArticleTitles.length !== 0) { + allArticleTitles = ARTICLE_TITLES.filter((el) => el.length <= 65); + return allArticleTitles; + } else return (allArticleTitles = []); } /* @@ -15,6 +19,17 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + let size = allArticleTitles.length; + for (let i = 0; i < size; i++) { + for (let j = i + 1; j < size; j++) { + if (allArticleTitles[i].length > allArticleTitles[j].length) { + let temp = allArticleTitles[i]; + allArticleTitles[i] = allArticleTitles[j]; + allArticleTitles[j] = temp; + } + } + } + return allArticleTitles[0]; } /* @@ -24,6 +39,9 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + return allArticleTitles.filter((element) => { + return /\d/.test(element); + }); } /* @@ -32,6 +50,12 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + let sum = 0; + + for (let i = 0; i < allArticleTitles.length; i++) { + sum = sum + allArticleTitles[i].length; + } + return Math.round(sum / allArticleTitles.length); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..86b23712 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,13 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + return closingPricesForAllStocks.map( + (element) => + +( + element.reduce((total, len) => total + len) / element.length + ).toFixed(2) + ); + } /* @@ -49,6 +55,8 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + + return closingPricesForAllStocks.map((element)=>+(element[4]-element[0]).toFixed(2)); } /* @@ -64,7 +72,15 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + + let highPrice=[]; + + stocks.forEach((stock,item) => { + highPrice.push( + `The highest price of ${stock.toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[item]).toFixed(2)}` + ); + }); + return highPrice; } From 75b4fa3833460c0fefb2b24780d2d3e9721eef92 Mon Sep 17 00:00:00 2001 From: leilafaez <79403281+leilafaez@users.noreply.github.com> Date: Wed, 21 Sep 2022 21:15:55 +0100 Subject: [PATCH 3/3] Update extra exercise --- 3-extra/1-factorial.js | 8 ++++++++ 3-extra/3-fibonacci.js | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..724b6a5e 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -10,6 +10,14 @@ function factorial(input) { // TODO + if (input === 0 || input === 1) { + return 1; + } else { + for (let i = input - 1; i >= 1; i--) { + input = input * i; + } + return input; + } } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..ad956db4 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,15 @@ */ function generateFibonacciSequence(n) { - // TODO + let fib = [0, 1]; + let data = []; + + for (let i = 2; i < n; i++) { + fib[i] = fib[i - 1] + fib[i - 2]; + data.push(fib[i]); + } + + return fib; } /* ======= TESTS - DO NOT MODIFY ===== */