diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..12d1916a 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 won't return anything, so the variable hello will return 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, there are no arguments being passed through the function so it will return Hello 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. 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 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 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: 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" 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 ===== */ diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..5cfc246b 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; } /* @@ -31,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; } diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..1158c61b 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) @@ -34,7 +34,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + // 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; } /* @@ -47,10 +61,28 @@ 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 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 @@ -64,7 +96,28 @@ function getPriceChanges(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; }