From f969bbaf03b07329a03d69308b6c45439ad77ffd Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 08:27:20 +0100 Subject: [PATCH 01/13] Update exercise.js --- 1-exercises/A-undefined/exercise.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..0a37eaca 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,12 +11,12 @@ // Example 1 let a; -console.log(a); +console.log(a); //Variable a didnt get a value assigned // Example 2 function sayHello() { - let message = "Hello"; + let message = "Hello"; //The function doesnt return anything } let hello = sayHello(); @@ -28,9 +28,9 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); //There is no string the function could take to fill the "user" // Example 4 let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]); //The last array position is 2 not 3 From 6ca81bf8be5b4a0ad3347140bfd9fe1d4f583610 Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 08:43:32 +0100 Subject: [PATCH 02/13] Update exercise.js --- 1-exercises/B-while-loop/exercise.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..5d769a96 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -5,8 +5,24 @@ The list of numbers should start with 0. n is being passed in as a parameter. */ -function evenNumbers(n) { - // TODO +function evenNumbers(n) +{ + let total = "0"; + let calc = 0; + + if (n === 0) + { + return console.log(""); + } + + for(let i = 1; i < n; i++) + { + calc = calc + 2; + + total = total + ", " + calc; + } + + return console.log(total); } evenNumbers(3); // should output 0,2,4 From 35f5fc2313248c9b07abd9bfa9649f3e6472e282 Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 09:07:14 +0100 Subject: [PATCH 03/13] Update exercise.js --- 1-exercises/C-while-loop-with-array/exercise.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..fd8b7d54 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" ]; -function findFirstJulyBDay(birthdays) { - // TODO +function findFirstJulyBDay(birthdays) +{ + let Arrlength = birthdays.length; + i = 0; + while(i < Arrlength) + { + if(birthdays[i].includes("July")) + { + return birthdays[i]; + } + i++; + } } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" From 9394d44a116beb351bdb83fc38fc6e7b7135082b Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 09:08:02 +0100 Subject: [PATCH 04/13] Update exercise.js *note* for doesnt work --- 1-exercises/C-while-loop-with-array/exercise.js | 1 + 1 file changed, 1 insertion(+) diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index fd8b7d54..7f38c433 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -20,6 +20,7 @@ function findFirstJulyBDay(birthdays) { let Arrlength = birthdays.length; i = 0; + while(i < Arrlength) { if(birthdays[i].includes("July")) From 196b4545345160542bc71ef2535afe03908fa369 Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 09:15:39 +0100 Subject: [PATCH 05/13] Update exercise.js --- 1-exercises/D-do-while/exercise.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..fc97e33b 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -6,10 +6,21 @@ Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0) */ -function evenNumbersSum(n) { - // TODO +function evenNumbersSum(n) +{ + total = 0; + i = 0; + + do + { + total += i * 2 + i++; + } + while ( i < n ); + + return total; } console.log(evenNumbersSum(3)); // should output 6 console.log(evenNumbersSum(0)); // should output 0 -console.log(evenNumbersSum(10)); // should output 90 \ No newline at end of file +console.log(evenNumbersSum(10)); // should output 90 From 7dba2184edb24488e908e0421af9e472f81cd9a1 Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 09:17:21 +0100 Subject: [PATCH 06/13] Update exercise1.js --- 1-exercises/E-for-loop/exercise1.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..afa2da1a 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,8 @@ // 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. From 09a51737be021e949d417faa3fe7ab400e2d2a7a Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 09:19:49 +0100 Subject: [PATCH 07/13] Update exercise2.js --- 1-exercises/E-for-loop/exercise2.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..301cd179 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,7 +27,10 @@ const AGES = [ ]; // TODO - Write for loop code here - +for (let i = 0; i < WRITERS.length; i++) +{ + console.log(WRITERS[i] + " is " + AGES[i] + " old") +} /* The output should look something like this: From 252cb701ebd806f311aef26238b8bb4e2e076ba9 Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 09:30:24 +0100 Subject: [PATCH 08/13] Update exercise.js *note* const is not needed before i in for of loop --- 1-exercises/F-for-of-loop/exercise.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..607e11d5 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,15 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (i of tubeStations) +{ + console.log(i); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for (i of str) +{ + console.log(i.toUpperCase()) +} From 4e4dd646034396cbf0b9642853ed14b2d23dd8d5 Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 09:46:05 +0100 Subject: [PATCH 09/13] Update 1-weather-report.js --- 2-mandatory/1-weather-report.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..3da0bda7 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -11,8 +11,15 @@ - Hint: you can call the temperatureService function from your function */ -function getTemperatureReport(cities) { - // TODO +function getTemperatureReport(cities) +{ + let TemperatureArray = []; + + for (let city of cities) + { + TemperatureArray.push("The temperature in " + city + " is " + temperatureService(city) + " degrees"); + } + return TemperatureArray; } @@ -60,4 +67,4 @@ test("should return a temperature report for the user's cities (alternate input) test("should return an empty array if the user hasn't selected any cities", () => { expect(getTemperatureReport([])).toEqual([]); -}); \ No newline at end of file +}); From 0f131803f6412f0154a8388048b7cb8ded94eb1e Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 09:50:15 +0100 Subject: [PATCH 10/13] Update 2-retrying-random-numbers.js --- 2-mandatory/2-retrying-random-numbers.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..2c861b77 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -9,8 +9,16 @@ function generateRandomNumber() { return Math.round(Math.random() * 100); } -function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop +function getRandomNumberGreaterThan50() +{ + let total; + do + { + (total = generateRandomNumber()); + } + while (total <= 50); + + return total; } /* ======= TESTS - DO NOT MODIFY ===== */ From 0c4bc993ca816c09cd8fbb185229c5c9dce9c1ec Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 10:59:43 +0100 Subject: [PATCH 11/13] Update 3-financial-times.js --- 2-mandatory/3-financial-times.js | 52 ++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..07cff2e4 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -1,11 +1,21 @@ /* Imagine you are working on the Financial Times web site! 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 + +function potentialHeadlines(allArticleTitles) +{ + let AcceptedArticles = [] + + for (let i = 0; i < allArticleTitles.length; i++) + { + if (allArticleTitles[i].length <= 65) + { + AcceptedArticles.push(allArticleTitles[i]); + } + } + return AcceptedArticles; } /* @@ -13,8 +23,12 @@ function potentialHeadlines(allArticleTitles) { 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 + +function titleWithFewestWords(allArticleTitles) +{ + const shorter = (left, right) => left.length <= right.length ? left : right; + + return allArticleTitles.reduce(shorter); } /* @@ -22,19 +36,37 @@ function titleWithFewestWords(allArticleTitles) { 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 + +function headlinesWithNumbers(allArticleTitles) +{ + let AcceptedArticles = []; + + for (let i = 0; i < allArticleTitles.length; i++) + { + if (/\d/.test(allArticleTitles[i])) + { + AcceptedArticles.push(allArticleTitles[i]); + } + } + return AcceptedArticles; } /* 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 -} +function averageNumberOfCharacters(allArticleTitles) +{ + let AllChar = 0; + let Average = 0; + for (let i = 0; i < allArticleTitles.length; i++) + { + AllChar += (allArticleTitles[i].length); + } + return Math.round(Average = AllChar / allArticleTitles.length); +} /* ======= List of Articles - DO NOT MODIFY ===== */ const ARTICLE_TITLES = [ From 7ff16db7d315e9bce84d8c9c138cf0e0b7bc1f97 Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 14:07:36 +0100 Subject: [PATCH 12/13] Update 4-stocks.js *note* const average = (closingPricesForAllStocks.map((array) => array.reduce((r, current) => r + current / array.length, 0))).map((element) => parseFloat(element.toFixed(2))); Should be used more often --- 2-mandatory/4-stocks.js | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..181e004e 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -33,8 +33,14 @@ 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 +//https://jrsinclair.com/articles/2019/five-ways-to-average-with-js-reduce/ +function getAveragePrices(closingPricesForAllStocks) +{ + //const reduce = r => i => a => a.reduce(r, i); + //conatiner = getting items => got items.reduce => add / length .map previous => round to 2 dec + const average = (closingPricesForAllStocks.map((array) => array.reduce((r, current) => r + current / array.length, 0))).map((element) => parseFloat(element.toFixed(2))); + + return average; } /* @@ -47,8 +53,12 @@ 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(closingPricesForAllStocks) +{ + const PriceChange = (closingPricesForAllStocks.map((array) => array[array.length - 1] - array[0])).map((element) => parseFloat(element.toFixed(2))); + + return PriceChange; } /* @@ -63,11 +73,26 @@ function getPriceChanges(closingPricesForAllStocks) { The stock ticker should be capitalised. The price should be shown with exactly 2 decimal places. */ -function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO -} +function highestPriceDescriptions(closingPricesForAllStocks, stocks) +{ + let HighestPriceText = []; + + for(let i = 0; i < closingPricesForAllStocks.length; i++) + { + let AllHighestPrices = 0; + for(let i2 = 0; i2 < closingPricesForAllStocks[i].length; i2++) + { + if(closingPricesForAllStocks[i][i2] > AllHighestPrices) + { + AllHighestPrices = closingPricesForAllStocks[i][i2]; + } + } + HighestPriceText.push("The highest price of " + STOCKS[i].toUpperCase() + " in the last 5 days was " + AllHighestPrices.toFixed(2)); + } + return HighestPriceText; +} /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the average price for each stock", () => { expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( From b504ad929f3ca97d8e3f51fe0a6628511971e985 Mon Sep 17 00:00:00 2001 From: Arbeits-Sachen <105115862+Arbeits-Sachen@users.noreply.github.com> Date: Mon, 15 Aug 2022 14:19:37 +0100 Subject: [PATCH 13/13] Update 1-factorial.js --- 3-extra/1-factorial.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..ca91d05d 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -8,8 +8,19 @@ Using a loop, complete the function below so it returns the factorial of the number being passed in. */ -function factorial(input) { - // TODO +function factorial(input) +{ + let OriginalInput = input; + let Total = 1; + for (let i = 0; i < input; i++) + { + if(input > 0) + { + Total *= OriginalInput; + OriginalInput--; + } + } + return Total; } /* ======= TESTS - DO NOT MODIFY ===== */