diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..121aa6b3 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,13 +12,15 @@ // Example 1 let a; console.log(a); - +//In this example, we see undefined because our variable doesn't have any value. // Example 2 function sayHello() { let message = "Hello"; + // In this example, We don't have return, so I would write return in this line. => return message; } + let hello = sayHello(); console.log(hello); @@ -29,8 +31,10 @@ function sayHelloToUser(user) { } sayHelloToUser(); - +//In this example,We do not have an argument because this is a function that has parameters, so outside the function it should have a value. +//I mean => sayHelloToUser(" we should write some string because od user")for example : sayHelloToUser("dear volunteer"); // Example 4 let arr = [1,2,3]; console.log(arr[3]); + //We have an array with the indexes o, 1, and 2 here, so there isn't index 3. we can Write arr[0] or arr[1] or arr [2] \ No newline at end of file diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..e94ee5e1 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,15 @@ */ function evenNumbers(n) { - // TODO + let num= 0 ; + let arr =[] + while(n > 0){ + arr.push(num) + num += 2 + n-- + } + console.log(arr) } - evenNumbers(3); // should output 0,2,4 evenNumbers(0); // should output nothing evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18 diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..ff7c4b55 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,16 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + + let i = 0; + + while( i <= birthdays.length ){ + if (birthdays[i].includes("July")){ + return birthdays[i] + } + i++ + } + } 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..5c2ecaa3 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 result = 0; + let i = 0 * 2; + do{ + result += i * 2; + i++; + }while(i < n) + + return result } 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..4842c01d 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..4bc28ab2 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(i = 0;i <= WRITERS.length;i++ ){ + const result=`${WRITERS[i]} is ${AGES[i]} years old ` + console.log(result) +} /* 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..7d83d7f8 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,13 @@ let tubeStations = [ "Tottenham Court Road" ]; +for(const element of tubeStations ){ + console.log(element) +} + // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for(const element of str){ + console.log(element.toLocaleUpperCase()) +} \ No newline at end of file diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..1e6224f7 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,12 @@ */ function getTemperatureReport(cities) { - // TODO + let temperature=[]; + // for(let i = 0;i < cities.length; i++){ + for(const city of cities){ + temperature.push(`The temperature in ${city} is ${temperatureService(city)} degrees`) + } + return temperature; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..a53b5699 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,7 +10,12 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let random = generateRandomNumber() ; + let i = 0; + do{ + random = generateRandomNumber() + }while ( random <= 50) + return random; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..1d181bef 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,7 +5,13 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + let arr =[]; + for (let article of allArticleTitles){ + if (article.length <= 65){ + arr.push(article) + } + } + return arr; } /* @@ -14,7 +20,11 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let arr = []; + for (let i = 0; i < allArticleTitles.length; i++) { + arr.push(allArticleTitles[i].split(" ").length); + } + return allArticleTitles[arr.indexOf(Math.min(...arr))]; } /* @@ -23,7 +33,17 @@ 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 arr=[] + for (let article of allArticleTitles){ + for (let char of article){ + if (char>="0" && char<="9"){ + arr.push (article); + break; + } + + } + } + return arr } /* @@ -31,7 +51,11 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let sum=0; + for (let article of allArticleTitles){ + sum+=article.length; + } + return Math.round(sum/allArticleTitles.length) } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..da8be378 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,16 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let averageArr=[]; + for (let closingPricesForStock of closingPricesForAllStocks){ + let sum =0; + for (let item of closingPricesForStock){ + sum=sum+item; + } + averageArr.push(Number((sum/closingPricesForStock.length).toFixed(2))); + + } + return averageArr; } /* @@ -48,7 +57,12 @@ 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 + let arr=[]; + for (let closingPricesForStock of closingPricesForAllStocks){ + arr.push(Number((closingPricesForStock[closingPricesForStock.length-1]-closingPricesForStock[0]).toFixed(2))); + + } + return arr; } /* @@ -64,7 +78,11 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let arr=[]; + for (let i=0;i { + const groupByGenre = cur.genre; + if (!acc[groupByGenre]) { + acc[groupByGenre] = []; + } + acc[groupByGenre].push(cur); + return acc; + }, {}); + const genreName = Object.keys(result); + const arr = []; + for (let i = 0; i < genreName.length; i++) { + arr.push( + result[genreName[i]].sort((a, b) => b.rating - a.rating)[0].title + ); + } + return arr; } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..24f7c13e 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,11 @@ */ function generateFibonacciSequence(n) { - // TODO + let fib = [0, 1]; + for (let i = 0; i < n - 2; i++) { + fib.push(fib[i] + fib[i + 1]); + } + return fib; } /* ======= TESTS - DO NOT MODIFY ===== */