diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..6e105e7f --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +// { +// // Use IntelliSense to learn about possible attributes. +// // Hover to view descriptions of existing attributes. +// // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 +// "version": "0.2.0", +// "configurations": [ +// { +// "type": "node", +// "request": "launch", +// "name": "Launch Program", +// "skipFiles": [ +// "/**" +// ], +// "program": "${workspaceFolder}/1-exercises/B-while-loop/exercise.js" +// } +// ] +// } \ No newline at end of file diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..4781ebab 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,13 +12,13 @@ // 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); @@ -28,9 +28,11 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +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] diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..181c197f 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,20 @@ */ 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..85fa320a 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -16,8 +16,19 @@ const BIRTHDAYS = [ "November 15th" ]; + 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" + +//includes + diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..1120a453 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,9 +7,16 @@ */ 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 console.log(evenNumbersSum(0)); // should output 0 -console.log(evenNumbersSum(10)); // should output 90 \ No newline at end of file +console.log(evenNumbersSum(10)); // should output 90 diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..ff9c100c 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,17 @@ // 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..224da9b1 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -28,6 +28,21 @@ 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) +} + + +//I did with forEach as well with same result + +// WRITERS.forEach((num1, index) => { +// const num2 = AGES[index]; +// const result = `${num1} is ${num2} 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..ea1962d2 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -10,7 +10,13 @@ let tubeStations = [ "Oxford Street", "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()) +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..196b5068 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,10 +12,14 @@ */ 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; } - /* ======= 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..62824e55 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..22c7ae97 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,16 +5,27 @@ 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; } + /* The editor of the FT likes short headlines with only a few words! Implement the function below, which returns the title with the fewest words. (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 +34,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 +52,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..2f2fa484 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,17 @@ 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 +58,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 +79,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..a45ee214 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 ===== */