From 9ccd4c0f4df3148c8beb0b728314f935d187e3da Mon Sep 17 00:00:00 2001 From: Chelle Date: Thu, 18 Aug 2022 09:14:12 +0100 Subject: [PATCH 1/6] extras done --- 1-exercises/A-undefined/exercise.js | 12 ++++++------ 1-exercises/B-while-loop/exercise.js | 11 +++++++++-- 1-exercises/C-while-loop-with-array/exercise.js | 8 +++++++- 1-exercises/D-do-while/exercise.js | 10 +++++++++- 1-exercises/E-for-loop/exercise1.js | 9 ++++++--- 1-exercises/E-for-loop/exercise2.js | 3 +++ 1-exercises/F-for-of-loop/exercise.js | 11 ++++++++++- 2-mandatory/2-retrying-random-numbers.js | 4 ++++ 8 files changed, 54 insertions(+), 14 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..de84a0d7 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,16 +11,16 @@ // Example 1 let a; -console.log(a); +console.log(a); //a has not been defined, just declared // Example 2 function sayHello() { - let message = "Hello"; + let message = "Hello"; //nothing is being returned } let hello = sayHello(); -console.log(hello); +console.log(hello); //it calls a function which doesn't return anything // Example 3 @@ -28,9 +28,9 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); //no parameter entered // Example 4 -let arr = [1,2,3]; -console.log(arr[3]); +let arr = [1, 2, 3]; +console.log(arr[3]); //indexes start at 0, there is no 3rd index set diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..920c6ea2 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,16 @@ */ function evenNumbers(n) { - // TODO + let i = 0; + let evens = []; + while (n > evens.length) { + evens[0] = 0; + i += 2; + evens.push(i); + } + return evens; } 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..bc6f67f1 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,13 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let i = 0; + while (i < birthdays.length) { + i++ + if (birthdays[i] === 'July 11th') { + return birthdays[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..153731dd 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,15 @@ */ function evenNumbersSum(n) { - // TODO + let sum = 0; + let i = 0; + do { + if (i % 2 === 0) { + sum += i * 3; + } + i++ + } while (i <= n); + return sum; } 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..9a0f0b84 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,12 @@ // Change the below code to use a for loop instead of a while loop. -let i = 0; -while(i < 26) { +// 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)); - 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..ba35a9c0 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,9 @@ const AGES = [ ]; // TODO - Write for loop code here +for (let i = 0; i < WRITERS.length && i < AGES.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..6fc78096 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -10,7 +10,16 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; - +let stations = ''; +for (tubes of tubeStations) { + stations += tubes + ', '; +} +console.log(stations); // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +let newStr = ''; +for (letter of str) { + newStr += letter.toUpperCase(); +} +console.log(newStr); diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..fcb55416 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,7 +11,11 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + do { + return generateRandomNumber(); + } while (generateRandomNumber() > 50); } +console.log(getRandomNumberGreaterThan50()); /* ======= TESTS - DO NOT MODIFY ===== */ From 3649a744ad2ee84910bf09c46cafe27067162cfd Mon Sep 17 00:00:00 2001 From: Chelle Date: Fri, 19 Aug 2022 08:57:28 +0100 Subject: [PATCH 2/6] most madatory and part 1 of stocks done --- 2-mandatory/1-weather-report.js | 17 ++++++++---- 2-mandatory/2-retrying-random-numbers.js | 6 ++-- 2-mandatory/3-financial-times.js | 35 ++++++++++++++++++++---- 2-mandatory/4-stocks.js | 30 +++++++++++++++++++- 4 files changed, 74 insertions(+), 14 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..8b09c355 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -10,16 +10,21 @@ For example, "The temperature in London is 10 degrees" - Hint: you can call the temperatureService function from your function */ - +ct = ['London', 'Paris', 'Barcelona', 'Dubai', 'Mumbai', 'São Paulo', 'Lagos']; function getTemperatureReport(cities) { - // TODO + + let statement = []; + for (let i = 0; i < cities.length; i++) { + let sentance = `The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees` + statement.push(sentance) + } + return statement; } - - +console.log(getTemperatureReport(ct)); /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { - let temparatureMap = new Map(); + let temparatureMap = new Map(); temparatureMap.set('London', 10); temparatureMap.set('Paris', 12); @@ -28,7 +33,7 @@ function temperatureService(city) { temparatureMap.set('Mumbai', 29); temparatureMap.set('São Paulo', 23); temparatureMap.set('Lagos', 33); - + return temparatureMap.get(city); } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index fcb55416..86c772bd 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -12,8 +12,10 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop do { - return generateRandomNumber(); - } while (generateRandomNumber() > 50); + randomNum = generateRandomNumber(); + } while (randomNum < 50); + + return randomNum; } console.log(getRandomNumberGreaterThan50()); diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..fe545517 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,7 +5,13 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + lessThan65 = [] + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length <= 65) { + lessThan65.push(allArticleTitles[i]); + } + } + return lessThan65; } /* @@ -14,7 +20,11 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length <= 50) { + return allArticleTitles[i]; + } + } } /* @@ -23,15 +33,27 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + withNums = [] + for (let i = 0; i < allArticleTitles.length; i++) { + if (/\d/.test(allArticleTitles[i])) { + withNums.push(allArticleTitles[i]); + } + } + return withNums; } + /* 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 sum = 0; + let numOfArticles = allArticleTitles.length; + for (let i = 0; i < allArticleTitles.length; i++) { + sum += allArticleTitles[i].length; + } + return Math.round(sum / numOfArticles); } @@ -49,7 +71,10 @@ const ARTICLE_TITLES = [ "The three questions that dominate investment", "Brussels urges Chile's incoming president to endorse EU trade deal", ]; - +// console.log(potentialHeadlines(ARTICLE_TITLES)) +// console.log(titleWithFewestWords(ARTICLE_TITLES)) +// console.log(headlinesWithNumbers(ARTICLE_TITLES)) +//console.log(averageNumberOfCharacters(ARTICLE_TITLES)) /* ======= TESTS - DO NOT MODIFY ===== */ test("should only return potential headlines", () => { diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..86f5dd1e 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -33,10 +33,38 @@ 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 getSum(arr) { + let sum = 0; + for (let i = 0; i < arr.length; i++) { + sum += arr[i]; + } + return sum; +} + function getAveragePrices(closingPricesForAllStocks) { - // TODO + let sum = 0; + let average = 0 + let result = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + //console.log(closingPricesForAllStocks[i]) + sum = getSum(closingPricesForAllStocks[i]) + //closingPricesForAllStocks[i].forEach(element => console.log(element)) + // for (let j = 0; j < closingPricesForAllStocks[i].length; j++) { + // //console.log(closingPricesForAllStocks[i][j]) + // sum += closingPricesForAllStocks[i][j]; + // //console.log(sum); + average = sum / 5; + // } + result.push(Math.round(average * 100) / 100); + } + return result; } + + +//console.log(sumArr(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[0])); +console.log(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)); /* 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 From dea66d008cb7cb84aedae0370603518193c97494 Mon Sep 17 00:00:00 2001 From: Chelle Date: Fri, 19 Aug 2022 09:31:55 +0100 Subject: [PATCH 3/6] mandatory done --- 2-mandatory/4-stocks.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 86f5dd1e..07bd0f86 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -60,11 +60,7 @@ function getAveragePrices(closingPricesForAllStocks) { } return result; } - - - -//console.log(sumArr(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[0])); -console.log(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)); +getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS); /* 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 @@ -75,10 +71,18 @@ console.log(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)); (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 findDifference(arr) { + return arr[4] - arr[0]; } +function getPriceChanges(closingPricesForAllStocks) { + let result = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + result.push(Number(findDifference(closingPricesForAllStocks[i]).toFixed(2))); + } + return result; +} +getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS) /* 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 @@ -92,9 +96,13 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let result = [] + for (let i = 0; i < closingPricesForAllStocks.length && i < stocks.length; i++) { + result.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${(Math.max(...closingPricesForAllStocks[i])).toFixed(2)}`) + } + return result; } - +console.log(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)); /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the average price for each stock", () => { From 52d6ec1dc72d1bbdd3b1ccef4ef70455a1438f16 Mon Sep 17 00:00:00 2001 From: Chelle Date: Fri, 19 Aug 2022 09:33:14 +0100 Subject: [PATCH 4/6] madatory comments removed --- 2-mandatory/4-stocks.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 07bd0f86..e36bf8e7 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -47,15 +47,8 @@ function getAveragePrices(closingPricesForAllStocks) { let average = 0 let result = []; for (let i = 0; i < closingPricesForAllStocks.length; i++) { - //console.log(closingPricesForAllStocks[i]) sum = getSum(closingPricesForAllStocks[i]) - //closingPricesForAllStocks[i].forEach(element => console.log(element)) - // for (let j = 0; j < closingPricesForAllStocks[i].length; j++) { - // //console.log(closingPricesForAllStocks[i][j]) - // sum += closingPricesForAllStocks[i][j]; - // //console.log(sum); average = sum / 5; - // } result.push(Math.round(average * 100) / 100); } return result; @@ -102,7 +95,7 @@ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { } return result; } -console.log(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)); +highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS); /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the average price for each stock", () => { From 98ae967d5181640a1a9a1f7bf77f5f7596ac7cf5 Mon Sep 17 00:00:00 2001 From: Chelle Date: Fri, 19 Aug 2022 10:49:15 +0100 Subject: [PATCH 5/6] 2/3 extras completed --- 3-extra/1-factorial.js | 11 ++++++++++- 3-extra/2-array-of-objects.js | 2 +- 3-extra/3-fibonacci.js | 13 +++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..82e25dfa 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,8 +9,17 @@ */ function factorial(input) { - // TODO + let result = 1; + if (input === 0 || input === 1) { + return input; + } else { + for (let i = input; i >= 1; i--) { + result = result * i; + } + } + return result; } +console.log(factorial(3)) /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..4c271638 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,7 +11,7 @@ */ function getHighestRatedInEachGenre(books) { - // TODO + } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..0636c69d 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,8 +14,17 @@ */ function generateFibonacciSequence(n) { - // TODO -} + if (n === 1) { + return [0, 1]; + } + else { + let result = generateFibonacciSequence(n - 1); + result.push(result[result.length - 1] + result[result.length - 2]); + return result; + } +}; +console.log(generateFibonacciSequence(10)); +//I used google /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the first 10 numbers in the Fibonacci Sequence", () => { From 3cd9935cca74728a86da7a3bc728b2881e27aeff Mon Sep 17 00:00:00 2001 From: Chelle Date: Fri, 19 Aug 2022 23:01:06 +0100 Subject: [PATCH 6/6] B-while-loop updated --- 1-exercises/B-while-loop/exercise.js | 2 +- 3-extra/2-array-of-objects.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index 920c6ea2..aa11d699 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -13,7 +13,7 @@ function evenNumbers(n) { i += 2; evens.push(i); } - return evens; + console.log(`${evens}`); } evenNumbers(3); // should output 0,2,4 diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index 4c271638..b393e7a4 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -9,7 +9,12 @@ Implement a function which takes the array of books as a parameter, and returns an array of book titles. Each title in the resulting array should be the highest rated book in its genre. */ +//get books by genre +//search highest rated using ge +//return highest rated +function getGenre(books){ +} function getHighestRatedInEachGenre(books) { }