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 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; } 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 ===== */