diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..063f456d 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 cities.map(x=>`The temperature in ${x} is ${temperatureService(x)} degrees`) } 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..a1356df6 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,16 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let myMin; + let myStr = '' + for (i in allArticleTitles){ + myMin = myMin || allArticleTitles[i].length + if (allArticleTitles[i].length < myMin){ + myMin = allArticleTitles[i].length + myStr = allArticleTitles[i] + } + } + return myStr } /* @@ -23,7 +32,7 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + return allArticleTitles.filter(x=>x.match(/\d+/g)) } /* @@ -31,7 +40,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(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 72d62f94..d1bc084c 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,20 @@ 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 + return closingPricesForAllStocks.map(x=>Number(calculateChange(x))) + } + /* 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 +77,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..16de4a91 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,7 +11,31 @@ */ function getHighestRatedInEachGenre(books) { - // TODO + 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 + } + + if (books[i].rating > maxNon && books[i].genre=="non-fiction"){ + maxNon = books[i].rating + nameNon = books[i].title + } + + if (books[i].rating > maxCook && books[i].genre=="cooking"){ + maxCook = books[i].rating + nameCook = books[i].title + } + + } + return [nameChil, nameNon, nameCook] } 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 {