From 8185f9d405e815a00d3bee01178809e6ef330c8b Mon Sep 17 00:00:00 2001 From: micheale2021 Date: Fri, 26 Aug 2022 18:16:14 +0100 Subject: [PATCH 1/4] complete exercise --- .vscode/settings.json | 3 +++ 1-exercises/A-undefined/exercise.js | 11 ++++++----- 1-exercises/B-while-loop/exercise.js | 12 +++++++++++- 1-exercises/C-while-loop-with-array/exercise.js | 11 +++++++++++ 1-exercises/D-do-while/exercise.js | 12 ++++++++++++ 1-exercises/E-for-loop/exercise1.js | 15 ++++++++++----- 1-exercises/E-for-loop/exercise2.js | 12 ++++++++++++ 1-exercises/F-for-of-loop/exercise.js | 10 +++++++++- 2-mandatory/1-weather-report.js | 13 ++++++++++++- 2-mandatory/2-retrying-random-numbers.js | 9 +++++++++ 2-mandatory/3-financial-times.js | 7 +++++++ 2-mandatory/4-stocks.js | 2 +- 12 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..30de70fe --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.acceptSuggestionOnEnter": "on" +} \ No newline at end of file diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..217afc66 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -10,12 +10,11 @@ */ // Example 1 -let a; +let a; // a is not assigned to any value console.log(a); - // Example 2 -function sayHello() { +function sayHello() { // function sayHello() is not returning anything let message = "Hello"; } @@ -28,9 +27,11 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); // function sayHelloToUser() is expecting a parameter but was never passed // Example 4 -let arr = [1,2,3]; +let arr = [1,2,3]; // Index 3 is not defined in the array. console.log(arr[3]); + + \ No newline at end of file diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..3a0472b2 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -4,11 +4,21 @@ Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-seperated string. The list of numbers should start with 0. n is being passed in as a parameter. */ - +// n % 2 == 0 function evenNumbers(n) { // TODO + let x = 1; + let y = 0; + let array = []; + while (x <= n) { + array.push(y); + x++; + y += 2; +} + console.log(array.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 diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..ee8a9132 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -16,8 +16,19 @@ const BIRTHDAYS = [ "November 15th" ]; + + function findFirstJulyBDay(birthdays) { // TODO + let count = 0; + let result = []; + while(count < birthdays.length){ + if(birthdays[count].includes("July")){ + result.push(birthdays[count]) + } + count ++; + } + return result[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..8d20b8dc 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,8 +8,20 @@ function evenNumbersSum(n) { // TODO + let sum = 0; + let x = 1; + let y = 0; + do { + sum = sum + y; + x++; + y += 2; + } while (x <= n) + return sum; } + + + 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 diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..0d339dc0 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,14 @@ // 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++; -} +// let i = 0; +// while(i < 26) { +// // console.log(String.fromCharCode(97 + i)); +// i++; +// } // The output shouldn't change. + +for (let i = 0; i < 26; i++) { + console.log(String.fromCharCode(97 + i)); + +} diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..e4bbddc8 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,18 @@ const AGES = [ ]; // TODO - Write for loop code here +let len; +if (WRITERS.length === AGES.length) { + len = WRITERS.length; + +} else { + return; +} + +for (let i = 0; i < len; 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..3e1a1ebd 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -10,7 +10,15 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; - +for (const 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 (const chr of str) { + + + console.log(chr.toUpperCase()); +} + diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..e3fc4dd7 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,18 @@ */ function getTemperatureReport(cities) { - // TODO + // TODO++ + let strArr = [] + + for (let i = 0; i < cities.length; i++) { + + // console.log(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees` ); + let textToDisplay = `The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`; + strArr.push(textToDisplay); + + + } + return strArr; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..db05e254 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,8 +11,16 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let result; + do { + result = generateRandomNumber(); + } while (result <= 50); + return result; + + } + /* ======= TESTS - DO NOT MODIFY ===== */ test("Returned value should always be greater than 50", () => { @@ -22,3 +30,4 @@ test("Returned value should always be greater than 50", () => { expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); }); + diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..39b3f2ac 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,6 +6,13 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + let articleTitles = [] + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length <= 65) { + articleTitles.push(allArticleTitles[i]); + } + } + return articleTitles; } /* diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..8937b571 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -76,7 +76,7 @@ test("should return the average price for each stock", () => { }); test("should return the price change for each stock", () => { - expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( + expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( [-6.2, -13.4, 23.9, -82.43, -162.77] ); }); From 8d1060fb4871bbb89e587f69bc6ffd254c4ebe56 Mon Sep 17 00:00:00 2001 From: micheale2021 Date: Tue, 30 Aug 2022 21:55:38 +0100 Subject: [PATCH 2/4] complete function titleWithFewestWords --- 2-mandatory/3-financial-times.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 39b3f2ac..cf6b145b 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -21,7 +21,14 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let shortestTitle = allArticleTitles[0]; + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].match(/\s+/g).length < + shortestTitle.match(/\s+/g).length) { + shortestTitle = allArticleTitles[i]; + } + } + return shortestTitle; } /* @@ -30,7 +37,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 titlesWithNum = []; + for (let i = 0; i < allArticleTitles.length; i++) { + if (/[0-9]/.test(allArticleTitles[i])) { // this reg exp checks numbers from 0 - 9 + titlesWithNum.push(allArticleTitles[i]); + } + } + return titlesWithNum; } /* From 81e773ec5d97f3ff0178ecf884dbfe3dad25df98 Mon Sep 17 00:00:00 2001 From: micheale2021 Date: Sun, 16 Oct 2022 22:23:56 +0100 Subject: [PATCH 3/4] update extra folder --- 3-extra/3-fibonacci.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..71512001 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -15,6 +15,14 @@ function generateFibonacciSequence(n) { // TODO + let fibonacciSequence = []; + fibonacciSequence[0] = 0; + fibonacciSequence[1] = 1; + for (i = 2; i < n; i++) { + fibonacciSequence[i] = fibonacciSequence[i - 2] + fibonacciSequence[i - 1]; + } + + return fibonacciSequence; } /* ======= TESTS - DO NOT MODIFY ===== */ From d177c8280ee5d1d9c45f4f164e703f78a9c34269 Mon Sep 17 00:00:00 2001 From: micheale2021 Date: Sun, 16 Oct 2022 22:28:20 +0100 Subject: [PATCH 4/4] modify mandatory and extra folder --- .vscode/settings.json | 3 ++- 2-mandatory/3-financial-times.js | 8 ++++++++ 2-mandatory/4-stocks.js | 15 +++++++++++++++ 3-extra/1-factorial.js | 9 +++++++++ 3-extra/2-array-of-objects.js | 8 ++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 30de70fe..17c2373b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "editor.acceptSuggestionOnEnter": "on" + "editor.acceptSuggestionOnEnter": "on", + "editor.wordWrap": "on" } \ No newline at end of file diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index cf6b145b..ba438473 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -52,6 +52,14 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + let average = 0; + let sum= 0; + for (let i = 0; i < allArticleTitles.length; i++) { + sum+= allArticleTitles[i].length; + + } + average = Math.round(sum / allArticleTitles.length); + return average; } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 8937b571..b9b1e374 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -35,6 +35,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let averageArray = []; + let averagePrice; + + for (const array of closingPricesForAllStocks) { + let num = 0; + for (const number of array) { + num += number; + } + + averagePrice = num / 5; + averageArray.push(parseFloat(averagePrice.toFixed(2))); + } + return averageArray; + + } /* diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..5202248b 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -10,6 +10,15 @@ function factorial(input) { // TODO + let result = 1; + + let i = input + do { + result *= i; + i--; + } while(i > 0); + + return result; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..dccc442f 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -12,6 +12,14 @@ function getHighestRatedInEachGenre(books) { // TODO + let highestRated = {}; + for(let book of books) { + if(highestRated[book.genre] === undefined || highestRated[book.genre].rating < book.rating) { + highestRated[book.genre] = book; + } + } + return Object.values(highestRated) + .map(book => book.title); }