From ccb71f22f447ade23b2cdd45d1edf6fb9c4560b8 Mon Sep 17 00:00:00 2001 From: mandy <109825538+m4ndycheung@users.noreply.github.com> Date: Sat, 4 Mar 2023 12:24:33 +0000 Subject: [PATCH 01/12] Added answers to A - undefined --- 1-exercises/A-undefined/exercise.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..24cf7593 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -13,6 +13,8 @@ let a; console.log(a); +// the variable a hasn't been initialised i.e. it's not been assigned an initial value + // Example 2 function sayHello() { @@ -22,6 +24,7 @@ function sayHello() { let hello = sayHello(); console.log(hello); +// the function doesn't return anything, so the variable hello is equal to undefined. // Example 3 function sayHelloToUser(user) { @@ -30,7 +33,12 @@ function sayHelloToUser(user) { sayHelloToUser(); +// the function needs an argument called user to be passed through it. +// in line 34, nothing is being passed through the function so it will be undefined. // Example 4 let arr = [1,2,3]; console.log(arr[3]); + +// so the array begins at index 0 and ends at index 2. +// there is no value assigned at index 3 so it'll return undefined. From 1132bf2fce4c6ce3f0a749ac7b8914c05be8a932 Mon Sep 17 00:00:00 2001 From: mandy <109825538+m4ndycheung@users.noreply.github.com> Date: Sat, 4 Mar 2023 12:31:00 +0000 Subject: [PATCH 02/12] Added answers to B-array-literals --- 1-exercises/B-array-literals/exercise.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..5bec732c 100644 --- a/1-exercises/B-array-literals/exercise.js +++ b/1-exercises/B-array-literals/exercise.js @@ -4,8 +4,8 @@ Declare some variables assigned to arrays of values */ -let numbers = []; // add numbers from 1 to 10 into this array -let mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares +let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // add numbers from 1 to 10 into this array +let mentors = ["Daniel", "Irina", "Rares"]; // Create an array with the names of the mentors: Daniel, Irina and Rares /* DO NOT EDIT BELOW THIS LINE From ed4c5a0db5d971a0fa2f2a9c1410bdcce531d770 Mon Sep 17 00:00:00 2001 From: mandy <109825538+m4ndycheung@users.noreply.github.com> Date: Sat, 4 Mar 2023 12:51:41 +0000 Subject: [PATCH 03/12] Added answers to C-array-get-set --- 1-exercises/C-array-get-set/exercise.js | 4 ++-- 1-exercises/C-array-get-set/exercises2.js | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/1-exercises/C-array-get-set/exercise.js b/1-exercises/C-array-get-set/exercise.js index 5ca911d5..9c1d01d7 100644 --- a/1-exercises/C-array-get-set/exercise.js +++ b/1-exercises/C-array-get-set/exercise.js @@ -5,11 +5,11 @@ */ function first(arr) { - return; // complete this statement + return arr[0]; // complete this statement } function last(arr) { - return; // complete this statement + return arr[arr.length - 1]; // complete this statement } /* diff --git a/1-exercises/C-array-get-set/exercises2.js b/1-exercises/C-array-get-set/exercises2.js index 6b6b007a..fad6e6b1 100644 --- a/1-exercises/C-array-get-set/exercises2.js +++ b/1-exercises/C-array-get-set/exercises2.js @@ -7,6 +7,8 @@ */ let numbers = [1, 2, 3]; // Don't change this array literal declaration +numbers.push(4); +numbers[0] = 1; /* DO NOT EDIT BELOW THIS LINE From 82efe2920e1763d828adf49ae327da062f3dcced Mon Sep 17 00:00:00 2001 From: mandy <109825538+m4ndycheung@users.noreply.github.com> Date: Sat, 4 Mar 2023 12:58:00 +0000 Subject: [PATCH 04/12] Added answer for D-for-loop --- 1-exercises/D-for-loop/exercise.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/1-exercises/D-for-loop/exercise.js b/1-exercises/D-for-loop/exercise.js index 081002b2..4861fdf2 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.js @@ -28,6 +28,13 @@ const AGES = [ // TODO - Write for loop code here +for (i = 0; i < WRITERS.length; i++) { + let writersName = WRITERS[i]; + let writersAge = AGES[i]; + + console.log(`${writersName} is ${writersAge} years old!`); +} + /* The output should look something like this: From a7934196da56f51415385c64d97403b262cd10f8 Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Tue, 7 Mar 2023 11:14:02 +0000 Subject: [PATCH 05/12] Amended some words in 1-exercises --- 1-exercises/A-undefined/exercise.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 24cf7593..5e4903f4 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -24,7 +24,7 @@ function sayHello() { let hello = sayHello(); console.log(hello); -// the function doesn't return anything, so the variable hello is equal to undefined. +// the function won't return anything, so the variable hello will return undefined. // Example 3 function sayHelloToUser(user) { @@ -34,7 +34,7 @@ function sayHelloToUser(user) { sayHelloToUser(); // the function needs an argument called user to be passed through it. -// in line 34, nothing is being passed through the function so it will be undefined. +// in line 34, there are no arguments being passed through the function so it will return undefined. // Example 4 let arr = [1,2,3]; From d79840c18847c451a0a0fa8914093ac8b183bc7a Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Tue, 7 Mar 2023 13:55:44 +0000 Subject: [PATCH 06/12] Answered E - 1-exercise --- 1-exercises/E-while-loop-with-array/exercise.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/1-exercises/E-while-loop-with-array/exercise.js b/1-exercises/E-while-loop-with-array/exercise.js index d584cd75..29fdf842 100644 --- a/1-exercises/E-while-loop-with-array/exercise.js +++ b/1-exercises/E-while-loop-with-array/exercise.js @@ -11,13 +11,21 @@ const BIRTHDAYS = [ "April 5th", "May 3rd", "July 11th", - "July 17th", + "July 17th",- "September 28th", "November 15th" ]; function findFirstJulyBDay(birthdays) { - // TODO + let i = 0; + + while (i < birthdays.length) { + if (birthdays[i].includes("July")) { + return birthdays[i]; + } + + i++; + } } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" From 2d97392ef264d37b40f7cba956fbbf281854171c Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Tue, 7 Mar 2023 14:40:19 +0000 Subject: [PATCH 07/12] Answered 2-1-weather report --- 2-mandatory/1-weather-report.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..8d0d0205 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,9 +12,18 @@ */ function getTemperatureReport(cities) { - // TODO -} + let temperatureReportResults = [ ]; + + for (i = 0; i < cities.length; i++) { + let city = cities[i]; + let temperature = temperatureService(city); + let reportPhrasing = `The temperature in ${city} is ${temperature} degrees`; + temperatureReportResults.push(reportPhrasing); + } + + return temperatureReportResults; +} /* ======= TESTS - DO NOT MODIFY ===== */ From 1cc9545a0dab6c1d0d2ffd2e43a0c41645ee96f1 Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Wed, 8 Mar 2023 16:20:51 +0000 Subject: [PATCH 08/12] Added some more answers to 2-financial-times --- 2-mandatory/2-financial-times.js | 35 +++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..3c7cf448 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -1,11 +1,19 @@ /* - Imagine you are working on the Financial Times web site! They have a list of article titles stored in an array. + Imagine you are working on the Financial Times website! They have a list of article titles stored in an array. The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less. Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + let titlesShortEnough = [ ]; + + for (const title of allArticleTitles) { + if(title.length <= 65) { + titlesShortEnough.push(title); + } + } + + return titlesShortEnough; } /* @@ -14,16 +22,33 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO -} + let smallestTitle = allArticleTitles[0]; + + for (i = 1; i < allArticleTitles.length; i++) { + + if (smallestTitle.split(" ").length > allArticleTitles[i].split(" ").length) { + smallestTitle = allArticleTitles[i]; + } + } + return smallestTitle; +} /* 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 + let headlinesWithNumbers = [ ]; + const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + + for (i = 0; i < allArticleTitles.length; i++) { + if(numbers.some(element => allArticleTitles[i].includes(element))) { + headlinesWithNumbers.push(allArticleTitles[i]); + } + } + return headlinesWithNumbers; } /* From 9dd94d13fc9cf7709a98d3646ee9885bf9eb1920 Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Wed, 8 Mar 2023 17:04:57 +0000 Subject: [PATCH 09/12] Finished 2-financial-times --- 2-mandatory/2-financial-times.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 3c7cf448..5cfc246b 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -56,7 +56,25 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + // To store the total characters per title + let totalCharactersPerTitle = [ ]; + + // To count the characters per title and add them to totalCharactersPerTitle + for (i = 0; i < allArticleTitles.length; i++) { + let countCharacters = allArticleTitles[i].length; + totalCharactersPerTitle.push(countCharacters); + } + + // To store the sum of total characters + let totalCharacters = 0; + totalCharactersPerTitle.forEach( item => { + totalCharacters += item; + }) + + // To work out the average and round to the nearest integer + let averageNumberOfCharacters = Math.round(totalCharacters / (allArticleTitles.length)); + + return averageNumberOfCharacters; } From dd87c0e93a309328af60ded5c9c3776247145fc4 Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Wed, 8 Mar 2023 17:59:38 +0000 Subject: [PATCH 10/12] Answered 3-stocks Part 1 --- 2-mandatory/3-stocks.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..7ce2bfa3 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -1,7 +1,7 @@ /* THESE EXERCISES ARE QUITE HARD. JUST DO YOUR BEST, AND COME WITH QUESTIONS IF YOU GET STUCK :) - Imagine we a working for a finance company. Below we have: + Imagine we are working for a finance company. Below we have: - an array of stock tickers - an array of arrays containing the closing price for each stock in each of the last 5 days. For example, CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[2] contains the prices for the last 5 days for STOCKS[2] (which is amzn) @@ -47,8 +47,22 @@ 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 getAveragePrices(closingPricesForAllStocks) { + // for storing averages of each company's stock over 5 days + let averagePriceOverFiveDays = [ ]; + + // This loop cycles through each stock, the i is the 5 day stock for AAPL, MSFT etc. + for (i = 0; i < closingPricesForAllStocks.length; i++) { + let totalStock = 0; + closingPricesForAllStocks[i].forEach((element) => { + totalStock += element; + }); + let calculateAverage = totalStock / closingPricesForAllStocks[i].length; + let roundAverage = Math.round(calculateAverage * 100) / 100; + averagePriceOverFiveDays.push(roundAverage); + } + + return averagePriceOverFiveDays; } /* From b7c0abf61a7bf9d73aac33fe3e5dd17ec3ae6959 Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Thu, 9 Mar 2023 17:33:23 +0000 Subject: [PATCH 11/12] Added last answer to stocks --- 1-exercises/A-undefined/exercise.js | 2 +- 2-mandatory/3-stocks.js | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 5e4903f4..12d1916a 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -34,7 +34,7 @@ function sayHelloToUser(user) { sayHelloToUser(); // the function needs an argument called user to be passed through it. -// in line 34, there are no arguments being passed through the function so it will return undefined. +// in line 34, there are no arguments being passed through the function so it will return Hello undefined. // Example 4 let arr = [1,2,3]; diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 7ce2bfa3..baedd02a 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -78,7 +78,28 @@ function getAveragePrices(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let maxPrices = [ ]; + let stockNames = stocks; + let maxStockDescriptions = [ ]; + + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let currentMax = closingPricesForAllStocks[i][0]; + + for (let j = 0; j < closingPricesForAllStocks[i].length; j++) { + let currentElement = closingPricesForAllStocks[i][j]; + + if (currentElement >= currentMax) { + currentMax = currentElement; + } + } + + let capitaliseStockNames = stockNames[i].toUpperCase(); + + maxPrices.push(currentMax.toFixed(2)); + maxStockDescriptions.push(`The highest price of ${capitaliseStockNames} in the last 5 days was ${maxPrices[i]}`) + } + + return maxStockDescriptions; } From a70410810676e6626907aaec1627c0dc782fbca2 Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Fri, 10 Mar 2023 13:29:35 +0000 Subject: [PATCH 12/12] Answered getPriceChanges in 3-stocks I accidentally wrote over this, and forgot to answer it so I've answered it now! --- 2-mandatory/3-stocks.js | 46 ++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index baedd02a..1158c61b 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -33,20 +33,6 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Solve the smaller problems, and then build those solutions back up to solve the larger problem. Functions can help with this! */ -function getAveragePrices(closingPricesForAllStocks) { - // TODO -} - -/* - 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 - - Takes this CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays) - - Returns an array containing the price change over the last 5 days for each stock. - For example, the first element of the resulting array should contain Apple’s (aapl) price change for the last 5 days. - In this example it would be: - (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 getAveragePrices(closingPricesForAllStocks) { // for storing averages of each company's stock over 5 days let averagePriceOverFiveDays = [ ]; @@ -65,6 +51,38 @@ function getAveragePrices(closingPricesForAllStocks) { return averagePriceOverFiveDays; } +/* + 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 + - Takes this CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays) + - Returns an array containing the price change over the last 5 days for each stock. + For example, the first element of the resulting array should contain Apple’s (aapl) price change for the last 5 days. + In this example it would be: + (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(array) { + + let changeInPrice = [ ]; + + for (i = 0; i < array.length; i++) { + let closingPricesArray = array; + let lengthOfInnerArray = closingPricesArray[i].length; + let fifthDayPrice = closingPricesArray[i][lengthOfInnerArray - 1]; + let firstDayPrice = closingPricesArray[i][0]; + + let fifthDayMinusFirstDayRounded = Math.round((fifthDayPrice - firstDayPrice) * 100) / 100; + + changeInPrice.push(fifthDayMinusFirstDayRounded); + } + + return changeInPrice; +} + + + /* 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