diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..0076f2e3 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -9,12 +9,12 @@ For each example, can you explain why we are seeing undefined? */ -// Example 1 +// Example 1 - a wasn't declared let a; console.log(a); -// Example 2 +// Example 2 - I didn't return any value function sayHello() { let message = "Hello"; } @@ -23,7 +23,7 @@ let hello = sayHello(); console.log(hello); -// Example 3 +// Example 3 - I didn't pass any parameter when I called the function sayHelloToUser function sayHelloToUser(user) { console.log(`Hello ${user}`); } @@ -31,6 +31,6 @@ function sayHelloToUser(user) { sayHelloToUser(); -// Example 4 +// Example 4 - the last number of my array is 3, but the position is 2, so when I call position 3 is undefined let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]); \ No newline at end of file diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..ef778493 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,22 @@ */ function evenNumbers(n) { - // TODO + let evenNumbersUntiln=0; + let i=1; + + while(i < n){ + evenNumbersUntiln = evenNumbersUntiln + "," + 2 * i; + i++; + } + + if(n !== 0){ + return evenNumbersUntiln; + } else { + return "nothing"; + } + } -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 +console.log(evenNumbers(3)); // should output 0,2,4 +console.log(evenNumbers(0)); // should output nothing +console.log(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..6a32a071 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -16,8 +16,18 @@ const BIRTHDAYS = [ "November 15th" ]; +let firstBirthday; + function findFirstJulyBDay(birthdays) { - // TODO + for(let i=0;i { - let usersCities = [ - "London", - "Paris", - "São Paulo" - ] - + } + + 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" + "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" - ] - + }); + + 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" + "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", () => { + }); + + test("should return an empty array if the user hasn't selected any cities", () => { expect(getTemperatureReport([])).toEqual([]); -}); \ No newline at end of file + }); \ No newline at end of file diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..6b4b8632 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,7 +10,13 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let randonNumber; + + do{ + randonNumber = generateRandomNumber(); + }while(randonNumber <= 50); + + return randonNumber; } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -21,4 +27,4 @@ test("Returned value should always be greater than 50", () => { expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); -}); +}); \ No newline at end of file diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..dee838da 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,39 +5,70 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO -} - -/* - 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 -} - -/* - 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 -} - -/* - 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 -} - - - -/* ======= List of Articles - DO NOT MODIFY ===== */ -const ARTICLE_TITLES = [ + let arrayFitArticles = []; + + for (let tittle of allArticleTitles) { + if (tittle.length <= 65) { + arrayFitArticles.push(tittle); + } + } + + return arrayFitArticles; + } + + /* + 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) { + let arrayFewestWords = []; + + for (let title of allArticleTitles) { + let eachTittle = title.split(" "); + arrayFewestWords.push(eachTittle); + } + + let titleFewestWords = arrayFewestWords.sort( + (a, b) => a.length - b.length + )[0]; + + return titleFewestWords.join(" "); + } + + /* + 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) { + let arrayNumTittle = []; + let condition = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]; + for (let title of allArticleTitles) { + if (condition.some((num) => title.includes(num))) { + arrayNumTittle.push(title); + } + } + return arrayNumTittle; + } + + /* + 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; + let totalArticles = allArticleTitles.length; + for (let article of allArticleTitles) { + let articleLength = article.length; + sum += articleLength; + } + let averageArticles = sum / totalArticles; + return Math.round(averageArticles); + } + + /* ======= List of Articles - DO NOT MODIFY ===== */ + const ARTICLE_TITLES = [ "Streaming wars drive media groups to spend more than $100bn on new content", "Amazon Prime Video India country head: streaming is driving a TV revolution", "Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights", @@ -48,34 +79,40 @@ const ARTICLE_TITLES = [ "Companies raise over $12tn in 'blockbuster' year for global capital markets", "The three questions that dominate investment", "Brussels urges Chile's incoming president to endorse EU trade deal", -]; - -/* ======= TESTS - DO NOT MODIFY ===== */ - -test("should only return potential headlines", () => { - expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual(new Set([ + ]; + + /* ======= TESTS - DO NOT MODIFY ===== */ + + test("should only return potential headlines", () => { + expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual( + new Set([ "British companies look to muscle in on US retail investing boom", "Libor to take firm step towards oblivion on New Year's Day", "Audit profession unattractive to new recruits, says PwC boss", - "The three questions that dominate investment" - ])); -}); - -test("should return an empty array for empty input", () => { + "The three questions that dominate investment", + ]) + ); + }); + + test("should return an empty array for empty input", () => { expect(potentialHeadlines([])).toEqual([]); -}); - -test("should return the title with the fewest words", () => { - expect(titleWithFewestWords(ARTICLE_TITLES)).toEqual("The three questions that dominate investment"); -}); - -test("should only return headlines containing numbers", () => { - expect(new Set(headlinesWithNumbers(ARTICLE_TITLES))).toEqual(new Set([ + }); + + test("should return the title with the fewest words", () => { + expect(titleWithFewestWords(ARTICLE_TITLES)).toEqual( + "The three questions that dominate investment" + ); + }); + + test("should only return headlines containing numbers", () => { + expect(new Set(headlinesWithNumbers(ARTICLE_TITLES))).toEqual( + new Set([ "Streaming wars drive media groups to spend more than $100bn on new content", - "Companies raise over $12tn in 'blockbuster' year for global capital markets" - ])); -}); - -test("should return the average number of characters in a headline", () => { + "Companies raise over $12tn in 'blockbuster' year for global capital markets", + ]) + ); + }); + + test("should return the average number of characters in a headline", () => { expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65); -}); + }); \ No newline at end of file diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..c4c09313 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 ]; /* @@ -26,7 +26,6 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ For example, the first element of the resulting array should contain Apple’s (aapl) average stock price for the last 5 days. The second element should be Microsoft's (msft) average price, and so on. The average value should be rounded to 2 decimal places, and should be a number (not a string) - Hint 1: To calculate the average of a set of values, you can add them together and divide by the number of values. So the average of 5, 10 and 20 is (5 + 10 + 20) / 3 = 11.67 Hint 2: If the problem seems complex, try breaking it down into smaller problems. @@ -34,7 +33,17 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let averageStock = []; + let sum = 0; + for (let stockPriceBrand of closingPricesForAllStocks) { + for (let unitStockPriceBrand of stockPriceBrand) { + sum += unitStockPriceBrand; + } + let averageAllStock = sum / stockPriceBrand.length; + averageStock.push(parseFloat(averageAllStock.toFixed(2))); + sum = 0; + } + return averageStock; } /* @@ -48,7 +57,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) { - // TODO + let priceChangeStock = []; + for (let stockPriceBrand of closingPricesForAllStocks) { + let priceChangeBrand = stockPriceBrand[4] - stockPriceBrand[0]; + let FormattedPriceChangeBrand = parseFloat(priceChangeBrand.toFixed(2)); + priceChangeStock.push(FormattedPriceChangeBrand); + priceChangeBrand = 0; + } + return priceChangeStock; } /* @@ -64,31 +80,54 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO -} + let ArraySentenceStock = []; + let highestStock = []; + let stockNameCapitalised = []; + + for (let stockPrice of closingPricesForAllStocks) { + let maxStockPrice = 0; + for (let compareStockPrice of stockPrice) { + if (compareStockPrice > maxStockPrice) { + maxStockPrice = compareStockPrice; + } + } + + highestStock.push(maxStockPrice.toFixed(2)); + } + for (let stockName of stocks) { + stockNameCapitalised.push(stockName.toUpperCase()); + } + + for (let i = 0; i < stocks.length; i++) { + let sentence = `The highest price of ${stockNameCapitalised[i]} in the last 5 days was ${highestStock[i]}`; + ArraySentenceStock.push(sentence); + } + + return ArraySentenceStock; +} /* ======= 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", + ]); });