diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..ead79e46 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,25 @@ +{ + "configurations": [ + + { + "args": [ + "--extensionDevelopmentPath=${workspaceFolder}" + ], + "name": "Launch Extension", + "outFiles": [ + "${workspaceFolder}/out/**/*.js" + ], + "preLaunchTask": "npm", + "request": "launch", + "type": "extensionHost" + }, + { + "type": "node", + "name": "Run Current File", + "request": "launch", + "program": "${workspaceFolder}/2-mandatory/3-stocks.js", + "stopOnEntry": true + + } + ] +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..b9a3e0b2 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,13 @@ */ function getTemperatureReport(cities) { - // TODO + let temparatureReport = []; + for (let item of cities) { + temparatureReport.push("The temperature in " + item + " is " + temperatureService(item) + " degrees"); + + // console.log(`The temperature in ${cities[i]} is ${temperatureService(cities[i])}`); + } + return temparatureReport; } @@ -46,6 +52,16 @@ test("should return a temperature report for the user's cities", () => { ]); }); +test("should return the same array length as input's length", () => { + let usersCities = [ + "London", + "Paris", + "São Paulo" + ] + + expect(getTemperatureReport(usersCities).length).toEqual(3); +}); + test("should return a temperature report for the user's cities (alternate input)", () => { let usersCities = [ "Barcelona", diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..bbe5d65f 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,7 +5,13 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + // let shortTitles = []; + // for (let item of allArticleTitles){ + // if (item.length <= 65) { + // shortTitles.push(item); + // } + // } + return allArticleTitles.filter(article => article.length <=65); } /* @@ -14,7 +20,17 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let shortestHeadline; + let fewestNumber; + for (let item of allArticleTitles) { + let numberOfWords = item.split(' ').length; + + if (fewestNumber === undefined || numberOfWords < fewestNumber) { + shortestHeadline = item; + fewestNumber = numberOfWords; + } + } + return shortestHeadline; } /* @@ -23,7 +39,13 @@ 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 linesWithNumbers = []; + for (let item of allArticleTitles) { + if (/\d/.test(item)) { + linesWithNumbers.push(item); + } + } + return linesWithNumbers; } /* @@ -31,7 +53,16 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + // number of characters in each article + // number of characters/number of articles (allArticleTitles.length) + + let charactersAmount = 0; + for (let item of allArticleTitles) { + let charSum = item.length; + charactersAmount +=charSum; + } + let average = charactersAmount / allArticleTitles.length; + return Math.round(average); } diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..7f40fd1e 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -34,7 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let averagePrices = []; + for (let item of closingPricesForAllStocks) { + let sum=0; + let average = 0; + for (let i of item) { + sum +=i; + } + average = sum/item.length; + averagePrices.push(Number(average.toFixed(2))); + } + + return averagePrices; } /* @@ -48,7 +59,13 @@ 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 priceChanges = []; + for (let item of closingPricesForAllStocks) { + let changed = item[item.length-1] - item[0]; + priceChanges.push(Number(changed.toFixed(2))); + } + + return priceChanges; } /* @@ -64,7 +81,24 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + + let priceDescrip = []; + let biggestPrice = 0; + let biggestPriceArray = []; + for (let item of closingPricesForAllStocks) { + let biggestPrice = 0; + for (let i of item) { + if (i > biggestPrice) { + biggestPrice = i; + } + } + biggestPriceArray.push(biggestPrice.toFixed(2)); + } + for (let i=0; i book.title); +} + + + /* ======= Book data - DO NOT MODIFY ===== */ const BOOKS = [ { diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..6bf060ba 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -15,6 +15,15 @@ function generateFibonacciSequence(n) { // TODO + const numberArray = [0, 1]; + let i = 2; + while (numberArray.length