From efcc1beffd2d7adf495e51f09d4e8c72c8a30ce4 Mon Sep 17 00:00:00 2001 From: Matildaako Date: Wed, 12 Jan 2022 13:00:26 +0000 Subject: [PATCH 1/8] Exercises complete --- 1-exercises/A-undefined/exercise.js | 7 +++++-- 1-exercises/B-while-loop/exercise.js | 10 +++++++++- 1-exercises/C-while-loop-with-array/exercise.js | 10 +++++++++- 1-exercises/D-do-while/exercise.js | 12 +++++++++++- 1-exercises/E-for-loop/exercise1.js | 4 +--- 1-exercises/E-for-loop/exercise2.js | 4 +++- 1-exercises/F-for-of-loop/exercise.js | 7 ++++++- 7 files changed, 44 insertions(+), 10 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..f4cbd6b8 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,16 +11,17 @@ // Example 1 let a; -console.log(a); +console.log(a); // a has been declared but not initialized, as it does not have a value, it returns undefined. // Example 2 function sayHello() { let message = "Hello"; -} +} let hello = sayHello(); console.log(hello); +// the function does not return anything // Example 3 @@ -29,8 +30,10 @@ function sayHelloToUser(user) { } sayHelloToUser(); +//when the function is called, there is no value given for the user argument, this results in the user variable being undefined // Example 4 let arr = [1,2,3]; console.log(arr[3]); +//arrays are zero indexed so the highest given index in this is 2 so there is no value in arr[3] diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..bc8a90f0 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,7 +6,15 @@ */ function evenNumbers(n) { - // TODO + let i = 0; + let result = []; + while (result.length < n) { + if (i % 2 === 0) { + result.push(i); + } + i++; + } + return result.toString(); } evenNumbers(3); // should output 0,2,4 diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..3e037780 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,15 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let julyDates = []; + let i = 0; + while (i < birthdays.length) { + if (birthdays[i].includes("July")) { + julyDates.push(birthdays[i]); + } + i++; + } + return julyDates[0]; } 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..bdb7b8a3 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,17 @@ */ function evenNumbersSum(n) { - // TODO + let numbers = []; + let sum = 0; + let i = 0; + do { + if (i % 2 === 0) { + sum += i; + numbers.push(i); + } + i++; + } while (numbers.length < n); + 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..1f81ac9d 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,7 @@ // Change the below code to use a for loop instead of a while loop. -let i = 0; -while(i < 26) { +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..77ea2f6b 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -26,7 +26,9 @@ const AGES = [ 49 ]; -// TODO - Write for loop code here +for (var 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..ad3197e9 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 station of tubeStations) { + console.log(station); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (let letter of str) { + console.log(letter.toUpperCase()) +} From 2e41398d485556699d5f92c64cc131c10b32cd35 Mon Sep 17 00:00:00 2001 From: Matildaako Date: Wed, 12 Jan 2022 13:15:04 +0000 Subject: [PATCH 2/8] Mandatory 1 complete --- 2-mandatory/1-weather-report.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..bd5f7d8b 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,11 @@ */ function getTemperatureReport(cities) { - // TODO + let weather = []; + for (var i = 0; i < cities.length; i++) { + weather.push(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`); + } + return weather; } From b351fbcf8b59b1ca84bf2ee0a4619897113552a5 Mon Sep 17 00:00:00 2001 From: Matildaako Date: Wed, 12 Jan 2022 14:11:55 +0000 Subject: [PATCH 3/8] Mandatory 2 complete --- 2-mandatory/2-retrying-random-numbers.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..1e9e6aa9 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 i = 0; + let x = 0; + do { + x = generateRandomNumber(i); + i++; + } while (x <= 50); + return x; } /* ======= TESTS - DO NOT MODIFY ===== */ From 2b6b8798a0962a41742050f1ed1571a9f2651085 Mon Sep 17 00:00:00 2001 From: Matildaako Date: Wed, 12 Jan 2022 15:14:35 +0000 Subject: [PATCH 4/8] Mandatory 3 complete --- 2-mandatory/3-financial-times.js | 37 ++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..1f396754 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-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 allowedTitles = []; + for (let title of allArticleTitles) { + if (title.length <= 65) { + allowedTitles.push(title); + } + } + return allowedTitles; } /* @@ -14,7 +20,17 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let allTitles = potentialHeadlines(allArticleTitles); + let wordsLength = Infinity; + let titleWords = ""; + for (let title of allTitles) { + let words = title.split(" "); + if (words.length < wordsLength) { + wordsLength = words.length; + titleWords = title; + } + } + return titleWords; } /* @@ -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 hasNumber = []; + for (title of allArticleTitles) { + if (title.match(/[0-9]/g)) { + hasNumber.push(title); + } + } + return hasNumber; } /* @@ -31,7 +53,14 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let titles = []; + for (title of allArticleTitles) { + titles.push(title.length); + } + let total = titles.reduce((a, b) => { + return a + b; + }) + return parseInt(total / titles.length) } From fabbba0dffa576ce074b35de97185277a142c0c3 Mon Sep 17 00:00:00 2001 From: Matildaako Date: Wed, 12 Jan 2022 19:08:54 +0000 Subject: [PATCH 5/8] Mandatory 4 complete --- 2-mandatory/4-stocks.js | 61 ++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..c3a230f3 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,15 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let closingPrices = closingPricesForAllStocks; + let averagePrices = []; + for (let prices of closingPrices) { + let total = prices.reduce((a, b) => { + return a + b; + }); + averagePrices.push(parseFloat((total / prices.length).toFixed(2))); + } + return averagePrices; } /* @@ -47,8 +55,17 @@ 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 rounded(num) { + return parseFloat(num.toFixed(2)); +} function getPriceChanges(closingPricesForAllStocks) { - // TODO + let closingPrices = closingPricesForAllStocks; + let priceChange = []; + for (let prices of closingPrices) { + let change = prices[prices.length - 1] - prices[0]; + priceChange.push(rounded(change)); + } + return priceChange; } /* @@ -64,31 +81,37 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO -} + let closingPrices = closingPricesForAllStocks; + let stock = stocks; + let highestArr = []; + for (let i = 0; i < closingPrices.length; i++) { + let highest = -Infinity; + for (let j = 0; j < closingPrices[i].length; j++) { + if (closingPrices[i][j] > highest) { + highest = closingPrices[i][j]; + } + } + highestArr.push(`The highest price of ${stock[i].toUpperCase()} in the last 5 days was ${highest.toFixed(2)}`); + } + return highestArr; +} /* ======= 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", + ]); }); From 70a85c50011a268954e38f41a85c17e4bd96d4d0 Mon Sep 17 00:00:00 2001 From: Matildaako Date: Thu, 13 Jan 2022 09:20:37 +0000 Subject: [PATCH 6/8] Extra 1 complete --- 3-extra/1-factorial.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..921056aa 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,7 +9,16 @@ */ function factorial(input) { - // TODO + let num = []; + let i = 1; + while (i <= input) { + num.push(i); + i++; + } + let result = num.reduce((a, b) => { + return a * b; + }) + return result; } /* ======= TESTS - DO NOT MODIFY ===== */ From bc69a6ec390922ea8788192d39d7cdb9755c9137 Mon Sep 17 00:00:00 2001 From: Matildaako Date: Thu, 13 Jan 2022 13:51:10 +0000 Subject: [PATCH 7/8] Extra 2 complete --- 3-extra/2-array-of-objects.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..c4ac61d2 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,7 +11,20 @@ */ function getHighestRatedInEachGenre(books) { - // TODO + let sorted = books.sort((a, b) => { + return a.rating === b.rating ? 0 : a.rating < b.rating ? 1 : -1; + }); + let i = 0; + let genres = []; + let topTitles = []; + while (genres.length < 3) { + if (!genres.includes(sorted[i].genre)) { + genres.push(sorted[i].genre); + topTitles.push(sorted[i].title); + } + i++; + } + return topTitles; } From 99b2ee79b2098a2bcdd2e171237098736ba47f14 Mon Sep 17 00:00:00 2001 From: Matildaako Date: Thu, 13 Jan 2022 17:40:24 +0000 Subject: [PATCH 8/8] Extra 3 complete --- 3-extra/3-fibonacci.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..6f6db1c8 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,13 @@ */ function generateFibonacciSequence(n) { - // TODO + let sequence = [0, 1]; + let i = 1; + while (sequence.length < n) { + sequence.push(sequence[i] + sequence[i - 1]); + i++; + } + return sequence; } /* ======= TESTS - DO NOT MODIFY ===== */