diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..2096366a 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); +we did not define a. // Example 2 @@ -21,6 +22,7 @@ function sayHello() { let hello = sayHello(); console.log(hello); +we need to call hello () not just hello // Example 3 @@ -29,8 +31,10 @@ function sayHelloToUser(user) { } sayHelloToUser(); +there is no variable and definition Here. // Example 4 let arr = [1,2,3]; console.log(arr[3]); +there is nothing for [3] it is empty diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..10a96c0c 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,8 +6,15 @@ */ function evenNumbers(n) { - // TODO + let i = 0; + +while (i<=n){ + if(i%2==0) + i++; + +} } +console.log(evenNumbers()); evenNumbers(3); // should output 0,2,4 evenNumbers(0); // should output nothing diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..81beea03 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,11 @@ 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" +console.log(findFirstJulyBDay(BIRTHDAYS)); // should output “July 11th” \ No newline at end of file diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..6d146b1f 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,9 +7,20 @@ */ function evenNumbersSum(n) { - // TODO -} + let counter = 1; + let sum = 0; + + do { + if (counter % 2 === 0) { + sum = sum + counter; + } + counter ++; + } while (counter <= n){ + return sum; +}} + + // TODO 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 diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..c7901d7b 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,8 @@ // 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++; + +for (let i = 0; i < 26, i++; ) { + console.log(String.fromCharCode(97)); } // 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..cc33f916 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,13 @@ const AGES = [ ]; // TODO - Write for loop code here +for (let i=0; i<(WRITERS.length); i++){ + +console .log ((`${WRITERS [i]} is ${AGES[i]} years old`)) +} + +// (WRITERS,AGES).forEach((WRITER, AGE) => console .log (`${WRITER } is ${AGE} years old`)); + /* 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..fdf1b133 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -10,7 +10,12 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; - +for (let tubeStation of tubeStations){ + console.log(tubeStation.toUpperCase("")); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (let tube of str){ + console.log(tube.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..6a9bf866 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,52 +12,51 @@ */ function getTemperatureReport(cities) { - // TODO + let result = []; + for (let i = 0; i < cities.length; i++) { + result.push( + `The temperature in ${cities[i]} is ${temperatureService( + cities[i] + )} degrees` + ); + } + return result; } - - /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { - let temparatureMap = new Map(); - - temparatureMap.set('London', 10); - temparatureMap.set('Paris', 12); - temparatureMap.set('Barcelona', 17); - temparatureMap.set('Dubai', 27); - temparatureMap.set('Mumbai', 29); - temparatureMap.set('São Paulo', 23); - temparatureMap.set('Lagos', 33); - - return temparatureMap.get(city); + let temparatureMap = new Map(); + + temparatureMap.set("London", 10); + temparatureMap.set("Paris", 12); + temparatureMap.set("Barcelona", 17); + temparatureMap.set("Dubai", 27); + temparatureMap.set("Mumbai", 29); + temparatureMap.set("São Paulo", 23); + temparatureMap.set("Lagos", 33); + + return temparatureMap.get(city); } test("should return a temperature report for the user's cities", () => { - let usersCities = [ - "London", - "Paris", - "São Paulo" - ] - - expect(getTemperatureReport(usersCities)).toEqual([ - "The temperature in London is 10 degrees", - "The temperature in Paris is 12 degrees", - "The temperature in São Paulo is 23 degrees" - ]); + let usersCities = ["London", "Paris", "São Paulo"]; + + expect(getTemperatureReport(usersCities)).toEqual([ + "The temperature in London is 10 degrees", + "The temperature in Paris is 12 degrees", + "The temperature in São Paulo is 23 degrees", + ]); }); test("should return a temperature report for the user's cities (alternate input)", () => { - let usersCities = [ - "Barcelona", - "Dubai" - ] - - expect(getTemperatureReport(usersCities)).toEqual([ - "The temperature in Barcelona is 17 degrees", - "The temperature in Dubai is 27 degrees" - ]); + let usersCities = ["Barcelona", "Dubai"]; + + expect(getTemperatureReport(usersCities)).toEqual([ + "The temperature in Barcelona is 17 degrees", + "The temperature in Dubai is 27 degrees", + ]); }); test("should return an empty array if the user hasn't selected any cities", () => { - expect(getTemperatureReport([])).toEqual([]); -}); \ No newline at end of file + expect(getTemperatureReport([])).toEqual([]); +}); diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..053c8e71 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -5,12 +5,18 @@ // This function shouldn't be changed function generateRandomNumber() { - console.log("Generating number..."); - return Math.round(Math.random() * 100); + console.log("Generating number..."); + return Math.round(Math.random() * 100); + } function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let number = generateRandomNumber(); + do { + number = generateRandomNumber(); + } while (number < 50); + return number; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..79a02d2d 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,6 +5,8 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { + + return allArticleTitles.filter(items=> items.length < 65); // TODO } @@ -14,6 +16,11 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { + let title = []; + for (let i = 0; i < allArticleTitles.length; i++) { + title.push(allArticleTitles [i].split (' ').length); + } + return allArticleTitles[title.indexOf(Math.min(...title))]; // TODO } @@ -24,13 +31,22 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + return allArticleTitles.filter((element) => + [...element].find((number) => number >= "0" && number <= "9") + ); } + /* The Financial Times wants to understand what the average number of characters in an article title is. Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { + let sum = 0; + for (let i = 0; i < allArticleTitles.length; i++) { + sum += allArticleTitles[i].length; + } + return Math.round(sum / allArticleTitles.length); // TODO } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..c79b592f 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -33,10 +33,28 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Solve the smaller problems, and then build those solutions back up to solve the larger problem. Functions can help with this! */ +function calculateAverage(newPrice) { + let sum = 0; + for (let price of newPrice) { + sum += price; + } + const average = sum / newPrice.length; + return average; +} function getAveragePrices(closingPricesForAllStocks) { - // TODO + // TODO + const myPrice = []; + for (let stock of closingPricesForAllStocks) { + const average = calculateAverage(stock); + const formattedAverage = Number(average.toFixed(2)); + myPrice.push(formattedAverage); + } + return myPrice; } +// let stocks = ["aapl", "msft", "amzn", "googl", "tsla"]; +// showStocks(stocks); +// changeInPrices(closingPricesLast5Days); /* We also want to see what the change in price is from the first day to the last day for each stock. Implement the below function, which @@ -48,6 +66,14 @@ 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) { + let myPrice1 = []; + for (let price of closingPricesForAllStocks) { + let firstDay = price[0]; + let lastDay = price[price.length - 1]; + let difference = lastDay - firstDay; + myPrice1.push(Number(difference.toFixed(2))); + } + return myPrice1; // TODO } @@ -64,6 +90,17 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { + let newPrice = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + newPrice.push( + `The highest price of ${stocks[ + i + ].toUpperCase()} in the last 5 days was ${Math.max( + ...closingPricesForAllStocks[i] + ).toFixed(2)}` + ); + } + return newPrice; // TODO } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..c72058b0 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,6 +9,11 @@ */ function factorial(input) { + let sum = 1; + for (i = 1; i <= input; i++) { + sum *= i; + } + return sum; // TODO } diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..5d3eedc6 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,6 +11,22 @@ */ function getHighestRatedInEachGenre(books) { + const result = books.reduce((acc, cur) => { + 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; // TODO } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..0c79ef92 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,24 +14,27 @@ */ function generateFibonacciSequence(n) { - // TODO + let fibo = [0, 1]; + for (let i = 0; i < n - 2; i++) { + fibo.push(fibo[i] + fibo[i + 1]); + } + return fibo; + // TODO } /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the first 10 numbers in the Fibonacci Sequence", () => { - expect(generateFibonacciSequence(10)).toEqual( - [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] - ); + expect(generateFibonacciSequence(10)).toEqual([ + 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, + ]); }); test("should return the first 5 numbers in the Fibonacci Sequence", () => { - expect(generateFibonacciSequence(5)).toEqual( - [0, 1, 1, 2, 3] - ); + expect(generateFibonacciSequence(5)).toEqual([0, 1, 1, 2, 3]); }); test("should return the first 15 numbers in the Fibonacci Sequence", () => { - expect(generateFibonacciSequence(15)).toEqual( - [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377] - ); -}); \ No newline at end of file + expect(generateFibonacciSequence(15)).toEqual([ + 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, + ]); +});