From 12c4a6f53282a50d80ea3d98314404f6dedd0d68 Mon Sep 17 00:00:00 2001 From: Dawit-Dev <100426510+Dawit-Dev@users.noreply.github.com> Date: Wed, 17 Aug 2022 00:20:07 +0100 Subject: [PATCH 1/2] Completed all the assignment --- 1-exercises/A-undefined/exercise.js | 7 +++ 1-exercises/B-while-loop/exercise.js | 19 +++++++- .../C-while-loop-with-array/exercise.js | 9 +++- 1-exercises/D-do-while/exercise.js | 13 +++++- 1-exercises/E-for-loop/exercise1.js | 10 +++-- 1-exercises/E-for-loop/exercise2.js | 5 ++- 1-exercises/F-for-of-loop/exercise.js | 7 ++- 2-mandatory/1-weather-report.js | 8 +++- 2-mandatory/2-retrying-random-numbers.js | 6 ++- 2-mandatory/3-financial-times.js | 44 +++++++++++++++++-- 2-mandatory/4-stocks.js | 32 ++++++++++++-- 3-extra/1-factorial.js | 15 ++++++- 3-extra/2-array-of-objects.js | 21 ++++++++- 3-extra/3-fibonacci.js | 10 ++++- 14 files changed, 185 insertions(+), 21 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..84824bd1 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -13,14 +13,18 @@ let a; console.log(a); +//A variable was declared but no value was assigned to it. // Example 2 function sayHello() { let message = "Hello"; } +//when a function does not return anything let hello = sayHello(); console.log(hello); + +// executing a function that produces no output // Example 3 @@ -30,7 +34,10 @@ function sayHelloToUser(user) { sayHelloToUser(); +// A function that is declared with a parameter but receives no arguments // Example 4 let arr = [1,2,3]; console.log(arr[3]); + +// The index does not exist in the array. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..25b6e915 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,24 @@ */ function evenNumbers(n) { - // TODO -} + + let arr = []; + let i = 0; + while (n > arr.length) { + if (i % 2 === 0) { + arr.push(i); + } + i++; + } + + console.log(arr.toString()); + } + + 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 + + + \ No newline at end of file diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..53047769 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,14 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let i = 0; + while (i < birthdays.length) { + let month = birthdays[i].substr(0, 3); + if (month === "Jul") { + 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..61de8876 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,18 @@ */ function evenNumbersSum(n) { - // TODO +let i = 0; + const evenNum = []; + do { + if (i % 2 === 0) { + evenNum.push(i); + } + i++ + } while (n > evenNum.length) + + let sum = evenNum.reduce((acc, curr) => acc + curr, 0); + return 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..d26eb564 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,13 @@ // Change the below code to use a for loop instead of a while loop. -let i = 0; -while(i < 26) { +// 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)); - 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..3ccd28c1 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 (let i = 0; i < WRITERS.length; i++) { + console.log(`${WRITERS[i]} is ${AGES[i]} 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..b62bd955 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 arr of tubeStations) { + console.log(arr); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + for (arr of str){ + console.log(arr.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..aa57e6ee 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,9 +12,15 @@ */ function getTemperatureReport(cities) { - // TODO + const temp = []; + for (let i = 0; i < cities.length; i++) { + temp.push('The temperature in ' + cities[i] + ' is ' + temperatureService(cities[i]) + ' degrees'); + } + return temp; } +// temp.push(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`) + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..5f886f2d 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,7 +10,11 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let i; + do { + i = generateRandomNumber(); + } while (i <= 50) + return i; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..a9e96629 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -4,34 +4,72 @@ The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less. Implement the function below, which will return a new array containing only article titles which will fit. */ + function potentialHeadlines(allArticleTitles) { - // TODO + let newArray = []; + for (let i = 0; i < allArticleTitles.length; i++){ + if (allArticleTitles[i].length <= 65) { + newArray.push(allArticleTitles[i]) + } + } + return newArray; } + /* 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 array = allArticleTitles[0].split(" ").length; + let spaceCount; + for (let i = 0; i < allArticleTitles.length; i++) { + wordCount = allArticleTitles[i].split(" ").length; + if (wordCount < array) { + array = wordCount; + spaceCount = i; + } + } + return allArticleTitles[spaceCount]; } + /* The editor of the FT has realised that headlines which have numbers in them get more clicks! Implement the function below to return a new array containing all the headlines which contain a number. (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + let result = []; + for (let i = 0; i < allArticleTitles.length; i++) { + let innersentece = allArticleTitles[i] + for (let j = 0; j < innersentece.length; j++){ + let value = innersentece[j] + if (!isNaN(value) && value != " ") + { + result.push(innersentece) + break; + } + } + } + return result; } /* 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) { // TODO + let charCountSum = 0; + for (let i = 0; i < allArticleTitles.length; i++){ + charCountSum += allArticleTitles[i].length; + } + let average = charCountSum / allArticleTitles.length; + return Math.round(average); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..e2784c77 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 arr = []; + let average = 0; + for (const stockPrices of closingPricesForAllStocks) { + let stockSum = 0; + for (const price of stockPrices){ + stockSum += price; + } + average = parseFloat((stockSum / stockPrices.length).toFixed(2)); + arr.push(average); + } + return arr; } /* @@ -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 changeArray = []; + for (const stockPrices of closingPricesForAllStocks) { + let priceChange = parseFloat((stockPrices[stockPrices.length - 1] - stockPrices [0]).toFixed(2)); + changeArray.push(priceChange); + } + return changeArray; } /* @@ -64,7 +79,18 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let result = [] + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let companyStock = closingPricesForAllStocks[i]; + let highestValue = companyStock[0]; + for (let j = 0; j < companyStock.length; j++) { + if (companyStock[j] > highestValue) { + highestValue = companyStock[j] + } + } + result[i] = "The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + highestValue.toFixed(2) + } + return result; } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..9e560d2c 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,9 +9,22 @@ */ function factorial(input) { - // TODO + let product = 1; + for (let i = input; i > 0; i--){ + product = product * i; + } + return product; + + // Alternate Solution + // var f = []; + // if (input == 0 || input == 1) + // return 1; + // if (f[input] > 0) + // return f[input]; + // return f[input] = factorial(input-1) * input; } + /* ======= TESTS - DO NOT MODIFY ===== */ test("3! should be 6", () => { diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..de73889f 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -10,8 +10,27 @@ Each title in the resulting array should be the highest rated book in its genre. */ + function getHighestRatedInEachGenre(books) { - // TODO + let allArray = []; + + let children = books.filter((obj) => obj.genre === "children"); + children = children.reduce((result, curr) => + result.rating > curr.rating ? result : curr + ); + + let nonFiction = books.filter((obj) => obj.genre === "non-fiction"); + nonFiction = nonFiction.reduce((result, curr) => + result.rating > curr.rating ? result : curr + ); + + let cooking = books.filter((obj) => obj.genre === "cooking"); + cooking = cooking.reduce((result, curr) => + result.rating > curr.rating ? result : curr + ); + + allArray.push(children.title, nonFiction.title, cooking.title); + return allArray; } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..6c4e7b52 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,8 +14,14 @@ */ function generateFibonacciSequence(n) { - // TODO -} + let i = 0; + let sequence = [0, 1] + while (sequence.length < n){ + sequence.push( (sequence[sequence.length - 2]) + sequence[sequence.length - 1]) + i++ + } + + return sequence} /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the first 10 numbers in the Fibonacci Sequence", () => { From faf413dc33be3ed48c875a705cc55218c7a210a6 Mon Sep 17 00:00:00 2001 From: Dawit-Dev <100426510+Dawit-Dev@users.noreply.github.com> Date: Sat, 20 Aug 2022 20:16:53 +0100 Subject: [PATCH 2/2] Modified mandatory --- 2-mandatory/3-financial-times.js | 44 +++++++------ 2-mandatory/4-stocks.js | 110 +++++++++++++++++-------------- 2 files changed, 85 insertions(+), 69 deletions(-) diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index a9e96629..9d16ff49 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,13 +6,15 @@ */ function potentialHeadlines(allArticleTitles) { - let newArray = []; - for (let i = 0; i < allArticleTitles.length; i++){ - if (allArticleTitles[i].length <= 65) { - newArray.push(allArticleTitles[i]) - } - } - return newArray; + return allArticleTitles.filter(title => title.length <= 65) + + // let newArray = []; + // for (let i = 0; i < allArticleTitles.length; i++){ + // if (allArticleTitles[i].length <= 65) { + // newArray.push(allArticleTitles[i]) + // } + // } + // return newArray; } @@ -42,19 +44,21 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - let result = []; - for (let i = 0; i < allArticleTitles.length; i++) { - let innersentece = allArticleTitles[i] - for (let j = 0; j < innersentece.length; j++){ - let value = innersentece[j] - if (!isNaN(value) && value != " ") - { - result.push(innersentece) - break; - } - } - } - return result; + return allArticleTitles.filter(headline => /[0-9]/.test(headline)); + + // let result = []; + // for (let i = 0; i < allHeadLineTitles.length; i++) { + // let innersentece = allArticleTitles[i] + // for (let j = 0; j < innersentece.length; j++){ + // let value = innersentece[j] + // if (!isNaN(value) && value != " ") + // { + // result.push(innersentece) + // break; + // } + // } + // } + // return result; } /* diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index e2784c77..94ebe230 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -11,11 +11,11 @@ const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"]; const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ - [179.19, 180.33, 176.28, 175.64, 172.99], // AAPL - [340.69, 342.45, 334.69, 333.20, 327.29], // MSFT - [3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN - [2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL - [1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA + [179.19, 180.33, 176.28, 175.64, 172.99], // AAPL + [340.69, 342.45, 334.69, 333.2, 327.29], // MSFT + [3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN + [2951.88, 2958.13, 2938.33, 2928.3, 2869.45], // GOOGL + [1101.3, 1093.94, 1067.0, 1008.87, 938.53], // TSLA ]; /* @@ -34,17 +34,17 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - let arr = []; - let average = 0; - for (const stockPrices of closingPricesForAllStocks) { - let stockSum = 0; - for (const price of stockPrices){ - stockSum += price; - } - average = parseFloat((stockSum / stockPrices.length).toFixed(2)); - arr.push(average); + let arr = []; + let average = 0; + for (const stockPrices of closingPricesForAllStocks) { + let stockSum = 0; + for (const price of stockPrices) { + stockSum += price; } - return arr; + average = parseFloat((stockSum / stockPrices.length).toFixed(2)); + arr.push(average); + } + return arr; } /* @@ -58,12 +58,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 changeArray = []; - for (const stockPrices of closingPricesForAllStocks) { - let priceChange = parseFloat((stockPrices[stockPrices.length - 1] - stockPrices [0]).toFixed(2)); - changeArray.push(priceChange); - } - return changeArray; + let changeArray = []; + for (const stockPrices of closingPricesForAllStocks) { + let priceChange = parseFloat( + (stockPrices[stockPrices.length - 1] - stockPrices[0]).toFixed(2) + ); + changeArray.push(priceChange); + } + return changeArray; } /* @@ -79,42 +81,52 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - let result = [] - for (let i = 0; i < closingPricesForAllStocks.length; i++) { - let companyStock = closingPricesForAllStocks[i]; - let highestValue = companyStock[0]; - for (let j = 0; j < companyStock.length; j++) { - if (companyStock[j] > highestValue) { - highestValue = companyStock[j] - } - } - result[i] = "The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + highestValue.toFixed(2) - } - return result; + const sortedPrices = closingPricesForAllStocks.map((prices) => + prices.sort((a, b) => b - a) + ); + return sortedPrices.map((price, index) => { + return `The highest price of ${stocks[ + index + ].toUpperCase()} in the last 5 days was ${price[0].toFixed(2)}`; + }); + // let result = [] + // for (let i = 0; i < closingPricesForAllStocks.length; i++) { + // let companyStock = closingPricesForAllStocks[i]; + // let highestValue = companyStock[0]; + // for (let j = 0; j < companyStock.length; j++) { + // if (companyStock[j] > highestValue) { + // highestValue = companyStock[j] + // } + // } + // result[i] = "The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + highestValue.toFixed(2) + // } + // return result; } - +// console.log( +// highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS) +// ); /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the average price for each stock", () => { - expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( - [176.89, 335.66, 3405.66, 2929.22, 1041.93] - ); + expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([ + 176.89, 335.66, 3405.66, 2929.22, 1041.93, + ]); }); test("should return the price change for each stock", () => { - expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( - [-6.2, -13.4, 23.9, -82.43, -162.77] - ); + expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([ + -6.2, -13.4, 23.9, -82.43, -162.77, + ]); }); test("should return a description of the highest price for each stock", () => { - expect(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)).toEqual( - [ - "The highest price of AAPL in the last 5 days was 180.33", - "The highest price of MSFT in the last 5 days was 342.45", - "The highest price of AMZN in the last 5 days was 3421.37", - "The highest price of GOOGL in the last 5 days was 2958.13", - "The highest price of TSLA in the last 5 days was 1101.30" - ] - ); + expect( + highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS) + ).toEqual([ + "The highest price of AAPL in the last 5 days was 180.33", + "The highest price of MSFT in the last 5 days was 342.45", + "The highest price of AMZN in the last 5 days was 3421.37", + "The highest price of GOOGL in the last 5 days was 2958.13", + "The highest price of TSLA in the last 5 days was 1101.30", + ]); });