diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..19e3f5d2 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,7 +11,7 @@ // Example 1 let a; -console.log(a); +console.log(a); // Variable a hasn't been assigned a value. // Example 2 @@ -19,7 +19,7 @@ function sayHello() { let message = "Hello"; } -let hello = sayHello(); +let hello = sayHello(); // Function sayHello is not returning anything, so hello variable is assigned to nothing, hence we get undefined. console.log(hello); @@ -28,9 +28,9 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser();// The function is missing argument, and is not defined what it will be returning. // Example 4 let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]);// The index value is out of range, since the length of arr is 3 and the last element index is 2. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..07e98836 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,15 @@ */ function evenNumbers(n) { - // TODO + let res = []; + let num = 0; + while(res.length < n) { + res.push(num); + num += 2; + } + return res.join(","); } 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 +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..df671506 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,11 @@ const BIRTHDAYS = [ ]; 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/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..e7deda07 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,13 @@ */ function evenNumbersSum(n) { - // TODO + let sum = []; + let num = 0; + do { + sum.push(num) + num += 2; + } while (sum.length < n) + return sum.reduce((tot, num) => tot + num); } 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..2a12e77e 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) { - console.log(String.fromCharCode(97 + i)); - i++; -} + +for(let i = 0; i < 26; i++) console.log(String.fromCharCode(97 + 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..6c6d0607 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,7 @@ const AGES = [ ]; // TODO - Write for loop code here +for (let 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..3ce1dd2b 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,8 @@ let tubeStations = [ "Tottenham Court Road" ]; +for(let tube of tubeStations) console.log(tube); // 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()); diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..79c69e68 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,7 @@ */ function getTemperatureReport(cities) { - // TODO + return cities.map(town => `The temperature in ${town} is ${temperatureService(town)} degrees`) } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..ee9d3d7c 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,7 +11,15 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let rand = generateRandomNumber(); + do { + if(rand > 50) { + return rand; + } + } while(rand <= 50) + return rand; } +console.log(getRandomNumberGreaterThan50()); /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..c92677b3 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,7 +5,9 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + return allArticleTitles.filter(title => { + if(title.length <= 65) return title; + }) } /* @@ -14,24 +16,26 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let num = Math.min(...allArticleTitles.map(title => title.split(" ").length)); + return allArticleTitles.filter(title => (title.split(" ").length === num) ? title : "").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) +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 + return allArticleTitles.filter(titleWithNum => (/\d/.test(titleWithNum)) ? titleWithNum : ""); } /* - 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. +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 + let arr = allArticleTitles.map(title => title.split("").length); + return Math.round(arr.reduce((tot, num) => tot + num)/arr.length); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..ead70e58 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,38 +34,45 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + return closingPricesForAllStocks.map(priceArr => { + return Number((priceArr.reduce((tot, num) => tot + num) / priceArr.length).toFixed(2)); + }) } /* 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 + - 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(closingPricesForAllStocks) { - // TODO + return closingPricesForAllStocks.map(priceArr => { + return Number((priceArr[priceArr.length - 1] - priceArr[0]).toFixed(2)); + }) } /* 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 - - Takes 2 parameters: - - the CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays) - - the STOCKS array - - Returns an array of strings describing what the highest price was for each stock. - For example, the first element of the array should be: "The highest price of AAPL in the last 5 days was 180.33" - The test will check for this exact string. + - Takes 2 parameters: + - the CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS array as input (remember, it's an array of arrays) + - the STOCKS array + - Returns an array of strings describing what the highest price was for each stock. + For example, the first element of the array should be: "The highest price of AAPL in the last 5 days was 180.33" + The test will check for this exact string. The stock ticker should be capitalised. The price should be shown with exactly 2 decimal places. -*/ + */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + return closingPricesForAllStocks.map((priceArr, i) => { + return `The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${Math.max(...priceArr).toFixed(2)}`; + }) } +console.log(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)); /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..eea47a4b 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -8,8 +8,10 @@ Using a loop, complete the function below so it returns the factorial of the number being passed in. */ +// Solved it using recursion instead of a loop function factorial(input) { - // TODO + if(input === 1) return 1; + return input * factorial(input - 1); } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..20376079 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -10,9 +10,6 @@ Each title in the resulting array should be the highest rated book in its genre. */ -function getHighestRatedInEachGenre(books) { - // TODO -} /* ======= Book data - DO NOT MODIFY ===== */ @@ -69,6 +66,23 @@ const BOOKS = [ }, ] +function getHighestRatedInEachGenre(books) { + let bookTitles = []; + let cookingRate = []; + let nonFictionRate = []; + let childrenRate = []; + for(let book of books) { + if(book.genre === "cooking") cookingRate.push(book.rating); + if(book.genre === "non-fiction") nonFictionRate.push(book.rating); + if(book.genre === "children") childrenRate.push(book.rating); + } + for(let book of books) { + if(book.genre === "cooking" && book.rating === Math.max(...cookingRate)) bookTitles.push(book.title); + if(book.genre === "non-fiction" && book.rating === Math.max(...nonFictionRate)) bookTitles.push(book.title); + if(book.genre === "children" && book.rating === Math.max(...childrenRate)) bookTitles.push(book.title); + } + return bookTitles; +} /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the highest rated book in each genre", () => { diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..a5ab9c2b 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,8 +14,19 @@ */ function generateFibonacciSequence(n) { - // TODO + let sequence = [0, 1]; + let num1 = 0; + let num2 = 1; + let num3 = 0; + for(let i = 0; i < n - 2; i++) { + num3 = num1 + num2; + sequence.push(num3); + num1 = num2; + num2 = num3; + } + return sequence; } +console.log(generateFibonacciSequence(10)) /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the first 10 numbers in the Fibonacci Sequence", () => {