From 0c7aaa4c5d4aaf07fa76a7b17c38b782daa331ed Mon Sep 17 00:00:00 2001 From: mathias02 Date: Mon, 11 Jul 2022 21:40:00 +0200 Subject: [PATCH 01/13] test loop --- 1-exercises/B-while-loop/exercise.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..1a6e0a98 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,7 +7,24 @@ function evenNumbers(n) { // TODO + const doing = []; + let i = 0; + + while(i < n){ + + let num = n[i]; + if(num !== 0){ + + } + const j = doing.push(num); + i++ + } + + return j; } +const j =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + +evenNumbers(j); evenNumbers(3); // should output 0,2,4 evenNumbers(0); // should output nothing From 2809a9ee3b6e52e975c10b74b9ae8b9e30da7214 Mon Sep 17 00:00:00 2001 From: mathias02 Date: Mon, 11 Jul 2022 23:06:41 +0200 Subject: [PATCH 02/13] add fix --- 1-exercises/B-while-loop/exercise.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index 1a6e0a98..b9e314fd 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -13,18 +13,17 @@ function evenNumbers(n) { while(i < n){ let num = n[i]; - if(num !== 0){ - - } - const j = doing.push(num); + if(num !== 0 && num % 2 === 0) return doing.push(num); + i++ } - return j; } + const j =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; evenNumbers(j); +console.log(evenNumbers(j)); evenNumbers(3); // should output 0,2,4 evenNumbers(0); // should output nothing From 805d91cb30fea4a610877ae924e9a51656cffcb5 Mon Sep 17 00:00:00 2001 From: mathias02 Date: Mon, 11 Jul 2022 23:53:47 +0200 Subject: [PATCH 03/13] add fix --- 1-exercises/B-while-loop/exercise.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b9e314fd..e6a2bd8b 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,16 +7,7 @@ function evenNumbers(n) { // TODO - const doing = []; - let i = 0; - while(i < n){ - - let num = n[i]; - if(num !== 0 && num % 2 === 0) return doing.push(num); - - i++ - } } From 5cc7bd43f0dfd465fd2119c49c6cc396c3a5f68b Mon Sep 17 00:00:00 2001 From: mathias02 Date: Tue, 12 Jul 2022 20:04:17 +0200 Subject: [PATCH 04/13] add fix --- 1-exercises/B-while-loop/exercise.js | 5 ++--- 1-exercises/E-for-loop/exercise1.js | 5 ++--- 1-exercises/E-for-loop/exercise2.js | 2 ++ 1-exercises/F-for-of-loop/exercise.js | 5 +++++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index e6a2bd8b..664845d4 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,14 +7,13 @@ function evenNumbers(n) { // TODO +const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; } -const j =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +console.log(evenNumbers(5)); -evenNumbers(j); -console.log(evenNumbers(j)); evenNumbers(3); // should output 0,2,4 evenNumbers(0); // should output nothing diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..45f4a91a 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. diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..9c44af4b 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -37,3 +37,5 @@ Jane Austen is 41 years old Bell Hooks is 63 years old Yukiko Motoya is 49 years old */ +let mix = WRITTERS.concat(AGES); +console.log(mix); \ No newline at end of file diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..b20e1d67 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -14,3 +14,8 @@ let tubeStations = [ // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for(let tubeStation of tubeStations){ + + console.log(tubeStation.toUpperCase()); +} From 9c442d0f12b21c64199673d9905aa22031e895e4 Mon Sep 17 00:00:00 2001 From: mathias02 Date: Tue, 12 Jul 2022 22:15:48 +0200 Subject: [PATCH 05/13] fix add --- 1-exercises/E-for-loop/exercise2.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 9c44af4b..081002b2 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -37,5 +37,3 @@ Jane Austen is 41 years old Bell Hooks is 63 years old Yukiko Motoya is 49 years old */ -let mix = WRITTERS.concat(AGES); -console.log(mix); \ No newline at end of file From 3797a38f2ce37313b76e99326748507304a53b22 Mon Sep 17 00:00:00 2001 From: mathias02 Date: Wed, 13 Jul 2022 12:58:13 +0200 Subject: [PATCH 06/13] add fix --- 1-exercises/C-while-loop-with-array/exercise.js | 10 ++++++++++ 1-exercises/D-do-while/exercise.js | 2 ++ 1-exercises/E-for-loop/exercise2.js | 7 +++++++ 1-exercises/F-for-of-loop/exercise.js | 8 +++++--- 4 files changed, 24 insertions(+), 3 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..d001775b 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,16 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + let i = 0; + + + while(i < BIRTHDAYS.length){ + + if(BIRTHDAYS[i] === "July 11th") return "July 11th"; + + 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..b4191591 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,9 @@ */ function evenNumbersSum(n) { + let sum = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // TODO + return sum.slice(0,n); } console.log(evenNumbersSum(3)); // should output 6 diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..11ba0cbf 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -28,6 +28,12 @@ const AGES = [ // TODO - Write for loop code here +for(let i = 0; i < WRITERS.length; i++){ + + console.log(WRITERS[i].concat(' is ' + AGES[i] + " years old ")); + +} + /* The output should look something like this: @@ -36,4 +42,5 @@ Zadie Smith is 40 years old Jane Austen is 41 years old Bell Hooks is 63 years old Yukiko Motoya is 49 years old + */ diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index b20e1d67..d6c8a451 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,11 +11,13 @@ let tubeStations = [ "Tottenham Court Road" ]; +for(let tubeStation of tubeStations){ + + console.log(tubeStation.toUpperCase()); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +str.toUpperCase(); -for(let tubeStation of tubeStations){ - console.log(tubeStation.toUpperCase()); -} From 883620d44cf209419570811b90bda7f27ee9ea12 Mon Sep 17 00:00:00 2001 From: mathias02 Date: Wed, 13 Jul 2022 16:24:41 +0200 Subject: [PATCH 07/13] fix add --- 1-exercises/B-while-loop/exercise.js | 12 +++++++++++- 1-exercises/D-do-while/exercise.js | 15 +++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index 664845d4..94d1a49c 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -9,10 +9,20 @@ function evenNumbers(n) { // TODO const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +let j = nums.slice(0, n); + +let i = 0; + +while( i < j.length){ + + console.log(j[i] * 2); + i ++; +} + +return j; } -console.log(evenNumbers(5)); evenNumbers(3); // should output 0,2,4 diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index b4191591..6450d953 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,9 +7,20 @@ */ function evenNumbersSum(n) { - let sum = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + // TODO - return sum.slice(0,n); + + let sums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + + let sliced = sums.slice(0, n); + let sum = 0; + + for(let slice of sliced){ + + sum += slice * 2; + } + + return sum; } console.log(evenNumbersSum(3)); // should output 6 From 69130a04bace0ea164535f1656a78d31f7fc0bed Mon Sep 17 00:00:00 2001 From: mathias02 Date: Wed, 13 Jul 2022 18:48:47 +0200 Subject: [PATCH 08/13] exercise A fixed --- 1-exercises/A-undefined/exercise.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..23ad61b2 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,15 +12,17 @@ // Example 1 let a; console.log(a); - +// Because there is no value is assigned to the variable a, // Example 2 function sayHello() { let message = "Hello"; } + let hello = sayHello(); console.log(hello); +// Since the function sayHello is missing the return statement invoking will it result in undefined // Example 3 @@ -30,7 +32,10 @@ function sayHelloToUser(user) { sayHelloToUser(); +// Since the function sayHelloToUser was defined with a parameter, however, invoking it will require an argument, and invoking it without an argument will result in undefined + // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// This operation will result in undefined because there is no index[3] in the array From 3b0c89aa89d85ab9784f4f4d76f6414c3154a8ea Mon Sep 17 00:00:00 2001 From: mathias02 Date: Thu, 14 Jul 2022 17:34:07 +0200 Subject: [PATCH 09/13] fix retrying file --- 2-mandatory/2-retrying-random-numbers.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..d1be4409 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,13 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + do{ + + let random = generateRandomNumber(); + + return random; + + }while(random > 50 ); } /* ======= TESTS - DO NOT MODIFY ===== */ From 0a66d4aba80132d9a619cb97d83dbb103795f39b Mon Sep 17 00:00:00 2001 From: Mathias02 Date: Thu, 14 Jul 2022 18:03:17 +0200 Subject: [PATCH 10/13] 2-retryng-random-numbers.js --- 2-mandatory/1-weather-report.js | 124 +++++++-------- 2-mandatory/2-retrying-random-numbers.js | 63 ++++---- 2-mandatory/3-financial-times.js | 173 +++++++++++---------- 2-mandatory/4-stocks.js | 188 +++++++++++------------ 4 files changed, 280 insertions(+), 268 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..1152d0c7 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -1,63 +1,63 @@ -/* - Imagine we're making a weather app! - - We have a list of cities that the user wants to track. - We also already have a temperatureService function which will take a city as a parameter and return a temparature. - - Implement the function below: - - take the array of cities as a parameter - - return an array of strings, which is a statement about the temperature of each city. - For example, "The temperature in London is 10 degrees" - - Hint: you can call the temperatureService function from your function -*/ - -function getTemperatureReport(cities) { - // TODO -} - - -/* ======= TESTS - DO NOT MODIFY ===== */ - -function temperatureService(city) { - let temparatureMap = new Map(); - - temparatureMap.set('London', 10); - temparatureMap.set('Paris', 12); - temparatureMap.set('Barcelona', 17); - temparatureMap.set('Dubai', 27); - temparatureMap.set('Mumbai', 29); - temparatureMap.set('São Paulo', 23); - temparatureMap.set('Lagos', 33); - - return temparatureMap.get(city); -} - -test("should return a temperature report for the user's cities", () => { - let usersCities = [ - "London", - "Paris", - "São Paulo" - ] - - expect(getTemperatureReport(usersCities)).toEqual([ - "The temperature in London is 10 degrees", - "The temperature in Paris is 12 degrees", - "The temperature in São Paulo is 23 degrees" - ]); -}); - -test("should return a temperature report for the user's cities (alternate input)", () => { - let usersCities = [ - "Barcelona", - "Dubai" - ] - - expect(getTemperatureReport(usersCities)).toEqual([ - "The temperature in Barcelona is 17 degrees", - "The temperature in Dubai is 27 degrees" - ]); -}); - -test("should return an empty array if the user hasn't selected any cities", () => { - expect(getTemperatureReport([])).toEqual([]); +/* + Imagine we're making a weather app! + + We have a list of cities that the user wants to track. + We also already have a temperatureService function which will take a city as a parameter and return a temparature. + + Implement the function below: + - take the array of cities as a parameter + - return an array of strings, which is a statement about the temperature of each city. + For example, "The temperature in London is 10 degrees" + - Hint: you can call the temperatureService function from your function +*/ + +function getTemperatureReport(cities) { + // TODO +} + + +/* ======= TESTS - DO NOT MODIFY ===== */ + +function temperatureService(city) { + let temparatureMap = new Map(); + + temparatureMap.set('London', 10); + temparatureMap.set('Paris', 12); + temparatureMap.set('Barcelona', 17); + temparatureMap.set('Dubai', 27); + temparatureMap.set('Mumbai', 29); + temparatureMap.set('São Paulo', 23); + temparatureMap.set('Lagos', 33); + + return temparatureMap.get(city); +} + +test("should return a temperature report for the user's cities", () => { + let usersCities = [ + "London", + "Paris", + "São Paulo" + ] + + expect(getTemperatureReport(usersCities)).toEqual([ + "The temperature in London is 10 degrees", + "The temperature in Paris is 12 degrees", + "The temperature in São Paulo is 23 degrees" + ]); +}); + +test("should return a temperature report for the user's cities (alternate input)", () => { + let usersCities = [ + "Barcelona", + "Dubai" + ] + + expect(getTemperatureReport(usersCities)).toEqual([ + "The temperature in Barcelona is 17 degrees", + "The temperature in Dubai is 27 degrees" + ]); +}); + +test("should return an empty array if the user hasn't selected any cities", () => { + expect(getTemperatureReport([])).toEqual([]); }); \ No newline at end of file diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index d1be4409..b90b3519 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -1,31 +1,32 @@ -/* - In the below example, we want to keep calling generateRandomNumber until we get a value that is > 50. - Implement this using a do-while loop. -*/ - -// This function shouldn't be changed -function generateRandomNumber() { - console.log("Generating number..."); - return Math.round(Math.random() * 100); -} - -function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop - do{ - - let random = generateRandomNumber(); - - return random; - - }while(random > 50 ); -} - -/* ======= TESTS - DO NOT MODIFY ===== */ - -test("Returned value should always be greater than 50", () => { - expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); - expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); - expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); - expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); - expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); -}); +/* + In the below example, we want to keep calling generateRandomNumber until we get a value that is > 50. + Implement this using a do-while loop. +*/ + +// This function shouldn't be changed +function generateRandomNumber() { + console.log("Generating number..."); + return Math.round(Math.random() * 100); +} + +function getRandomNumberGreaterThan50() { + // TODO - implement using a do-while loop + do{ + + let random = generateRandomNumber(); + return random; + + }while(random > 50 ); + + +} + +/* ======= TESTS - DO NOT MODIFY ===== */ + +test("Returned value should always be greater than 50", () => { + expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); + expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); + expect(getRandomNumberGreaterThan50()).toBeGreaterThan(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..5a856772 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -1,81 +1,92 @@ -/* - 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 -} - -/* - The editor of the FT likes short headlines with only a few words! - 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 -} - -/* - 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 -} - -/* - 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 -} - - - -/* ======= List of Articles - DO NOT MODIFY ===== */ -const ARTICLE_TITLES = [ - "Streaming wars drive media groups to spend more than $100bn on new content", - "Amazon Prime Video India country head: streaming is driving a TV revolution", - "Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights", - "British companies look to muscle in on US retail investing boom", - "Libor to take firm step towards oblivion on New Year's Day", - "Audit profession unattractive to new recruits, says PwC boss", - "Chinese social media users blast Elon Musk over near miss in space", - "Companies raise over $12tn in 'blockbuster' year for global capital markets", - "The three questions that dominate investment", - "Brussels urges Chile's incoming president to endorse EU trade deal", -]; - -/* ======= TESTS - DO NOT MODIFY ===== */ - -test("should only return potential headlines", () => { - expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual(new Set([ - "British companies look to muscle in on US retail investing boom", - "Libor to take firm step towards oblivion on New Year's Day", - "Audit profession unattractive to new recruits, says PwC boss", - "The three questions that dominate investment" - ])); -}); - -test("should return an empty array for empty input", () => { - expect(potentialHeadlines([])).toEqual([]); -}); - -test("should return the title with the fewest words", () => { - expect(titleWithFewestWords(ARTICLE_TITLES)).toEqual("The three questions that dominate investment"); -}); - -test("should only return headlines containing numbers", () => { - expect(new Set(headlinesWithNumbers(ARTICLE_TITLES))).toEqual(new Set([ - "Streaming wars drive media groups to spend more than $100bn on new content", - "Companies raise over $12tn in 'blockbuster' year for global capital markets" - ])); -}); - -test("should return the average number of characters in a headline", () => { - expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65); -}); +/* + 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 + let char = allArticleTitles.slice(0, 65); + let title = []; + for(let i = 0; i < char.length; i++){ + + if(char[i] === 65){ + + title.push(char[i]); + } + } + return title; + +} + +/* + The editor of the FT likes short headlines with only a few words! + 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 +} + +/* + 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 +} + +/* + 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 +} + + + +/* ======= List of Articles - DO NOT MODIFY ===== */ +const ARTICLE_TITLES = [ + "Streaming wars drive media groups to spend more than $100bn on new content", + "Amazon Prime Video India country head: streaming is driving a TV revolution", + "Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights", + "British companies look to muscle in on US retail investing boom", + "Libor to take firm step towards oblivion on New Year's Day", + "Audit profession unattractive to new recruits, says PwC boss", + "Chinese social media users blast Elon Musk over near miss in space", + "Companies raise over $12tn in 'blockbuster' year for global capital markets", + "The three questions that dominate investment", + "Brussels urges Chile's incoming president to endorse EU trade deal", +]; + +/* ======= TESTS - DO NOT MODIFY ===== */ + +test("should only return potential headlines", () => { + expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual(new Set([ + "British companies look to muscle in on US retail investing boom", + "Libor to take firm step towards oblivion on New Year's Day", + "Audit profession unattractive to new recruits, says PwC boss", + "The three questions that dominate investment" + ])); +}); + +test("should return an empty array for empty input", () => { + expect(potentialHeadlines([])).toEqual([]); +}); + +test("should return the title with the fewest words", () => { + expect(titleWithFewestWords(ARTICLE_TITLES)).toEqual("The three questions that dominate investment"); +}); + +test("should only return headlines containing numbers", () => { + expect(new Set(headlinesWithNumbers(ARTICLE_TITLES))).toEqual(new Set([ + "Streaming wars drive media groups to spend more than $100bn on new content", + "Companies raise over $12tn in 'blockbuster' year for global capital markets" + ])); +}); + +test("should return the average number of characters in a headline", () => { + expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65); +}); diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..4944932c 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -1,94 +1,94 @@ -/* - 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: - - 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) -*/ - -/* ======= Stock data - DO NOT MODIFY ===== */ -const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"]; - -const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ - [179.19, 180.33, 176.28, 175.64, 172.99], // AAPL - [340.69, 342.45, 334.69, 333.20, 327.29], // MSFT - [3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN - [2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL - [1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA -]; - -/* - We want to understand what the average price over the last 5 days for each stock is. - 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 average price over the last 5 days for each stock. - For example, the first element of the resulting array should contain Apple’s (aapl) average stock price for the last 5 days. - The second element should be Microsoft's (msft) average price, and so on. - The average value should be rounded to 2 decimal places, and should be a number (not a string) - - Hint 1: To calculate the average of a set of values, you can add them together and divide by the number of values. - So the average of 5, 10 and 20 is (5 + 10 + 20) / 3 = 11.67 - Hint 2: If the problem seems complex, try breaking it down into smaller problems. - 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 -} - -/* - 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 - The price change value should be rounded to 2 decimal places, and should be a number (not a string) -*/ -function getPriceChanges(closingPricesForAllStocks) { - // TODO -} - -/* - 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. - The stock ticker should be capitalised. - The price should be shown with exactly 2 decimal places. -*/ -function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO -} - - -/* ======= TESTS - DO NOT MODIFY ===== */ -test("should return the average price for each stock", () => { - expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( - [176.89, 335.66, 3405.66, 2929.22, 1041.93] - ); -}); - -test("should return the price change for each stock", () => { - expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( - [-6.2, -13.4, 23.9, -82.43, -162.77] - ); -}); - -test("should return a description of the highest price for each stock", () => { - expect(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)).toEqual( - [ - "The highest price of AAPL in the last 5 days was 180.33", - "The highest price of MSFT in the last 5 days was 342.45", - "The highest price of AMZN in the last 5 days was 3421.37", - "The highest price of GOOGL in the last 5 days was 2958.13", - "The highest price of TSLA in the last 5 days was 1101.30" - ] - ); -}); +/* + 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: + - 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) +*/ + +/* ======= Stock data - DO NOT MODIFY ===== */ +const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"]; + +const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ + [179.19, 180.33, 176.28, 175.64, 172.99], // AAPL + [340.69, 342.45, 334.69, 333.20, 327.29], // MSFT + [3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN + [2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL + [1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA +]; + +/* + We want to understand what the average price over the last 5 days for each stock is. + 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 average price over the last 5 days for each stock. + For example, the first element of the resulting array should contain Apple’s (aapl) average stock price for the last 5 days. + The second element should be Microsoft's (msft) average price, and so on. + The average value should be rounded to 2 decimal places, and should be a number (not a string) + + Hint 1: To calculate the average of a set of values, you can add them together and divide by the number of values. + So the average of 5, 10 and 20 is (5 + 10 + 20) / 3 = 11.67 + Hint 2: If the problem seems complex, try breaking it down into smaller problems. + 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 +} + +/* + 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 + The price change value should be rounded to 2 decimal places, and should be a number (not a string) +*/ +function getPriceChanges(closingPricesForAllStocks) { + // TODO +} + +/* + 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. + The stock ticker should be capitalised. + The price should be shown with exactly 2 decimal places. +*/ +function highestPriceDescriptions(closingPricesForAllStocks, stocks) { + // TODO +} + + +/* ======= TESTS - DO NOT MODIFY ===== */ +test("should return the average price for each stock", () => { + expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( + [176.89, 335.66, 3405.66, 2929.22, 1041.93] + ); +}); + +test("should return the price change for each stock", () => { + expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( + [-6.2, -13.4, 23.9, -82.43, -162.77] + ); +}); + +test("should return a description of the highest price for each stock", () => { + expect(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)).toEqual( + [ + "The highest price of AAPL in the last 5 days was 180.33", + "The highest price of MSFT in the last 5 days was 342.45", + "The highest price of AMZN in the last 5 days was 3421.37", + "The highest price of GOOGL in the last 5 days was 2958.13", + "The highest price of TSLA in the last 5 days was 1101.30" + ] + ); +}); From 5d386e1b3271e07963d4ffe669e4226671946522 Mon Sep 17 00:00:00 2001 From: Mathias02 Date: Fri, 15 Jul 2022 22:56:11 +0200 Subject: [PATCH 11/13] files fixed --- 2-mandatory/1-weather-report.js | 9 +++++++ 2-mandatory/2-retrying-random-numbers.js | 9 ++++--- 2-mandatory/3-financial-times.js | 31 ++++++++++++++++++++++++ 2-mandatory/4-stocks.js | 22 ++++++++++++++++- 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index 1152d0c7..5a1af238 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,6 +12,15 @@ */ function getTemperatureReport(cities) { + + let temp = temperatureService(city); + const towns = cities.forEach((city) =>{ + + city = `The temperature in ${city} is ${temp} degrees`; + }) + + return city; + // TODO } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index b90b3519..6412e2ca 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,13 +11,16 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + + let random = 0; + do{ - let random = generateRandomNumber(); - return random; + random = generateRandomNumber(); - }while(random > 50 ); + }while(random < 50); + return random; } diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 5a856772..d41fcc53 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -26,6 +26,14 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + let chars = allArticleTitles.slice(0, 65); + let elements = []; + let i = 0; + while(i < chars.length){ + + elements.push(chars[i]); + } + return elements; } /* @@ -35,6 +43,16 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + + const nums = allArticleTitles.filter((article) => article === number); + + let toBeClicked = []; + + for(let num of nums){ + + toBeClicked.push(num[i]) + } + return toBeClicked; } /* @@ -43,6 +61,19 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + let aver = []; + + for(let i = 0; i < allArticleTitles.length; i++){ + + for(let j = 0; j < allArticleTitles[i].length; j++){ + + let jan = aver.push(allArticleTitles[i][j].join()); + let charCode = jan.charCodeAt(allArticleTitles[i][j].join()); + charCode / allArticleTitles[i][j].join().length; + } + } + return charCode; + } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 4944932c..77f62619 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -33,8 +33,20 @@ 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! */ +const stocking = STOCKS.forEach((el) =>{ + return el; +}) + function getAveragePrices(closingPricesForAllStocks) { // TODO + let dblArray = []; + for(let i = 0; i < closingPricesForAllStocks.length; i++){ + for(let j = 0; j < closingPricesForAllStocks[i]; j++){ + dblArray.push(closingPricesForAllStocks[i][j]); + dblArray.concat(stocking[i]); + } + } + return dblArray; } /* @@ -49,8 +61,16 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + let result = 0; + closingPricesForAllStocks.forEach((stock) =>{ + stock.forEach((item) =>{ + + result = item[0] - item[item.length - 1]; + result.toFixed(2); + }) + }) + return result; } - /* 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 From 1e7f6cb8c55f0b9876354ab0f3de3a3ab116a16a Mon Sep 17 00:00:00 2001 From: Mathias02 Date: Fri, 15 Jul 2022 23:02:08 +0200 Subject: [PATCH 12/13] last fix --- 2-mandatory/1-weather-report.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index 5a1af238..485a2bda 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -14,7 +14,7 @@ function getTemperatureReport(cities) { let temp = temperatureService(city); - const towns = cities.forEach((city) =>{ + cities.forEach((city) =>{ city = `The temperature in ${city} is ${temp} degrees`; }) From 76c7d4ed8feff63e8c6b2cbf5500503591a1f555 Mon Sep 17 00:00:00 2001 From: Mathias02 Date: Fri, 15 Jul 2022 23:23:57 +0200 Subject: [PATCH 13/13] stock fixed --- 2-mandatory/4-stocks.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 77f62619..a0c7605e 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -85,6 +85,16 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + const highestPrices = closingPricesForAllStocks.forEach((price) =>{ + price.forEach((element) => { + + if(element.Math.max()){ + + return `The highest price of ${stocks} in the last 5 days was ${element.Math.max().toFixed(2)}` + } + }) + }) + return highestPrices.toUpperCase(); }