From 78266b5d6cef0be7f28a2664901936d4ecb97deb Mon Sep 17 00:00:00 2001 From: MacOS Date: Fri, 2 Dec 2022 00:15:42 +0000 Subject: [PATCH 1/5] feat: finishing 3 tasks --- 2-mandatory/1-weather-report.js | 6 +++++- 2-mandatory/2-retrying-random-numbers.js | 5 +++++ 2-mandatory/3-financial-times.js | 18 ++++++++++++++---- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..328283c3 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,11 @@ */ function getTemperatureReport(cities) { - // TODO + let report = [] + for (i in cities){ + report.push(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`) + } + return report } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..06cda02c 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,11 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let randNum = generateRandomNumber() + do { + randNum = generateRandomNumber() ; +} while (randNum <= 50); +return randNum } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..4e413f03 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,7 +5,7 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + return allArticleTitles.filter(x=>x.length<=65) } /* @@ -14,7 +14,15 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let myMin = 100; + let myStr = '' + for (i in allArticleTitles){ + if (allArticleTitles[i].lengthx.match(/\d+/g)) } /* @@ -31,7 +39,9 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + + let sumOfChar = allArticleTitles.map(x=>x.length) + return Math.round(sumOfChar.reduce(function(a, b) { return a + b; }, 0)/allArticleTitles.length) } From 1cbe450932e84a9e9a51937a9459fc6e32b7b034 Mon Sep 17 00:00:00 2001 From: MacOS Date: Sun, 4 Dec 2022 08:17:25 +0000 Subject: [PATCH 2/5] feat: madatory and part extra --- 2-mandatory/4-stocks.js | 31 +++++++++++++++++++++++-------- 3-extra/1-factorial.js | 6 +++++- 3-extra/2-array-of-objects.js | 31 ++++++++++++++++++++++++++++++- 3-extra/3-fibonacci.js | 10 ++++++++-- 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..d99f1828 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,9 +34,12 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let averArr = [] + for ( let i of closingPricesForAllStocks){ + averArr.push(Number((i.reduce((accumulator, value) => {return accumulator + value})/i.length).toFixed(2))) + } + return averArr } - /* 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 @@ -47,10 +50,19 @@ function getAveragePrices(closingPricesForAllStocks) { (Apple's price on the 5th day) - (Apple's price on the 1st day) = 172.99 - 179.19 = -6.2 The price change value should be rounded to 2 decimal places, and should be a number (not a string) */ -function getPriceChanges(closingPricesForAllStocks) { - // TODO -} - +function calculateChange (prices){ + return (prices[prices.length-1]-prices[0]).toFixed(2) + } + + function getPriceChanges(closingPricesForAllStocks) { + let changArr = [] + for ( let i of closingPricesForAllStocks){ + let change = calculateChange(i) + changArr.push(Number(change)) + } + return changArr + } + /* As part of a financial report, we want to see what the highest price was for each stock in the last 5 days. Implement the below function, which @@ -64,10 +76,13 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let myarr = [] + for (let i in closingPricesForAllStocks){ + myarr.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[i]).toFixed(2)}`) + } + return myarr } - /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the average price for each stock", () => { expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..aabe9b77 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,7 +9,11 @@ */ function factorial(input) { - // TODO + let factor = 1 + for (let i = 1; i<=input; i++ ){ + factor *=i + } + return factor } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..d342bd5b 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,7 +11,36 @@ */ function getHighestRatedInEachGenre(books) { - // TODO + console.log(books) + let maxChil = 0; + let nameChil = '' + let myarr = [] + let maxNon = 0; + let nameNon = '' + let maxCook = 0; + let nameCook = '' + for (const i in books){ + if (books[i].rating > maxChil && books[i].genre=="children"){ + maxChil = books[i].rating + nameChil = books[i].title + } + myarr.push(nameChil) + } + for (const i in books){ + if (books[i].rating > maxNon && books[i].genre=="non-fiction"){ + maxNon = books[i].rating + nameNon = books[i].title + } + myarr.push(nameNon) + } + for (const i in books){ + if (books[i].rating > maxCook && books[i].genre=="cooking"){ + maxCook = books[i].rating + nameCook = books[i].title + } + myarr.push(nameCook) + } + return myarr } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..147e9f1b 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,8 +14,14 @@ */ function generateFibonacciSequence(n) { - // TODO -} + let fibSec = [0,1] + let summa = 0 + for (let i = 0; i { From 569f352793681c8bf493c7c1dcd6ad36b66cb517 Mon Sep 17 00:00:00 2001 From: MacOS Date: Sun, 4 Dec 2022 08:26:45 +0000 Subject: [PATCH 3/5] feat: finishing extra --- 3-extra/2-array-of-objects.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index d342bd5b..3a009284 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,7 +11,6 @@ */ function getHighestRatedInEachGenre(books) { - console.log(books) let maxChil = 0; let nameChil = '' let myarr = [] @@ -24,23 +23,23 @@ function getHighestRatedInEachGenre(books) { maxChil = books[i].rating nameChil = books[i].title } - myarr.push(nameChil) + } for (const i in books){ if (books[i].rating > maxNon && books[i].genre=="non-fiction"){ maxNon = books[i].rating nameNon = books[i].title } - myarr.push(nameNon) + } for (const i in books){ if (books[i].rating > maxCook && books[i].genre=="cooking"){ maxCook = books[i].rating nameCook = books[i].title } - myarr.push(nameCook) + } - return myarr + return [nameChil, nameNon, nameCook] } From 1e657183026afa72408025726b704e718cb62b73 Mon Sep 17 00:00:00 2001 From: MacOS Date: Mon, 5 Dec 2022 10:56:36 +0000 Subject: [PATCH 4/5] feat: updating some functions with map --- 2-mandatory/1-weather-report.js | 10 +++++----- 2-mandatory/3-financial-times.js | 4 ++-- 2-mandatory/4-stocks.js | 13 +++++++------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index 328283c3..063f456d 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,11 +12,11 @@ */ function getTemperatureReport(cities) { - let report = [] - for (i in cities){ - report.push(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`) - } - return report + // let report = [] + // for (i in cities){ + // report.push(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`) + // } + return cities.map(x=>`The temperature in ${x} is ${temperatureService(x)} degrees`) } diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 4e413f03..9e70c185 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -40,8 +40,8 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { - let sumOfChar = allArticleTitles.map(x=>x.length) - return Math.round(sumOfChar.reduce(function(a, b) { return a + b; }, 0)/allArticleTitles.length) + // let sumOfChar = allArticleTitles.map(x=>x.length) + return Math.round(allArticleTitles.reduce(function(a, b) { return a + b.length }, 0)/allArticleTitles.length) } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index d99f1828..d1bc084c 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -55,12 +55,13 @@ function calculateChange (prices){ } function getPriceChanges(closingPricesForAllStocks) { - let changArr = [] - for ( let i of closingPricesForAllStocks){ - let change = calculateChange(i) - changArr.push(Number(change)) - } - return changArr + // let changArr = [] + // for ( let i of closingPricesForAllStocks){ + // let change = calculateChange(i) + // changArr.push(Number(change)) + // } + // return changArr + return closingPricesForAllStocks.map(x=>Number(calculateChange(x))) } /* From 7a645b8f944679a54a47baf64674b0b5b7c82370 Mon Sep 17 00:00:00 2001 From: MacOS Date: Mon, 5 Dec 2022 12:37:37 +0000 Subject: [PATCH 5/5] feat: updating 2-array task --- 2-mandatory/3-financial-times.js | 5 +++-- 3-extra/2-array-of-objects.js | 8 ++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 9e70c185..a1356df6 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -14,10 +14,11 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - let myMin = 100; + let myMin; let myStr = '' for (i in allArticleTitles){ - if (allArticleTitles[i].length maxNon && books[i].genre=="non-fiction"){ maxNon = books[i].rating nameNon = books[i].title } - - } - for (const i in books){ + if (books[i].rating > maxCook && books[i].genre=="cooking"){ maxCook = books[i].rating nameCook = books[i].title