From e2a76a792d29eec1d37b1d4df1b0350f642cb145 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 4 Mar 2023 10:52:13 +0000 Subject: [PATCH 01/14] Exercise A completed --- 1-exercises/A-undefined/exercise.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..fc66b4fd 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,7 +12,7 @@ // Example 1 let a; console.log(a); - +// a is not assigned a value // Example 2 function sayHello() { @@ -21,7 +21,7 @@ function sayHello() { let hello = sayHello(); console.log(hello); - +//There is no value passed to the function sayHello // Example 3 function sayHelloToUser(user) { @@ -30,7 +30,11 @@ function sayHelloToUser(user) { sayHelloToUser(); +//no value passed to the function + // Example 4 let arr = [1,2,3]; console.log(arr[3]); + +//There is no value at index 3 \ No newline at end of file From 8ead1a5b7623db69e958ade753fc69669e64a5b3 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 4 Mar 2023 10:54:42 +0000 Subject: [PATCH 02/14] Exercise B completed --- 1-exercises/B-array-literals/exercise.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..0062c63b 100644 --- a/1-exercises/B-array-literals/exercise.js +++ b/1-exercises/B-array-literals/exercise.js @@ -4,8 +4,8 @@ Declare some variables assigned to arrays of values */ -let numbers = []; // add numbers from 1 to 10 into this array -let mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares +let numbers = [1,2,3,4,5,6,7,8,9,10]; // add numbers from 1 to 10 into this array +let mentors = ["Daniel", "Irina", "Rares"]; // Create an array with the names of the mentors: Daniel, Irina and Rares /* DO NOT EDIT BELOW THIS LINE From 8987441ff747e5c78234e5d5e677ca9a6f1cebff Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 4 Mar 2023 11:01:02 +0000 Subject: [PATCH 03/14] Exercise C part 1 --- 1-exercises/C-array-get-set/exercise.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-exercises/C-array-get-set/exercise.js b/1-exercises/C-array-get-set/exercise.js index 5ca911d5..8e793582 100644 --- a/1-exercises/C-array-get-set/exercise.js +++ b/1-exercises/C-array-get-set/exercise.js @@ -5,11 +5,11 @@ */ function first(arr) { - return; // complete this statement + return arr[0]; // complete this statement } function last(arr) { - return; // complete this statement + return arr[arr.length - 1]; ; // complete this statement } /* From 24b581f902a87f79f5c680e35c0236715d2cd0bf Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 4 Mar 2023 11:06:44 +0000 Subject: [PATCH 04/14] Exercise 3 part 2 --- 1-exercises/C-array-get-set/exercises2.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/1-exercises/C-array-get-set/exercises2.js b/1-exercises/C-array-get-set/exercises2.js index 6b6b007a..58595e00 100644 --- a/1-exercises/C-array-get-set/exercises2.js +++ b/1-exercises/C-array-get-set/exercises2.js @@ -8,6 +8,8 @@ let numbers = [1, 2, 3]; // Don't change this array literal declaration +numbers[3] = numbers.push(4) +//The first value is 1??? /* DO NOT EDIT BELOW THIS LINE --------------------------- */ From 59153954a6160e08a5f2147741eb3637d077f618 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 4 Mar 2023 11:16:07 +0000 Subject: [PATCH 05/14] Exercise D --- 1-exercises/D-for-loop/exercise.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/1-exercises/D-for-loop/exercise.js b/1-exercises/D-for-loop/exercise.js index 081002b2..e2c2ef74 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.js @@ -26,6 +26,14 @@ const AGES = [ 49 ]; +AGES.forEach(k => { + WRITERS.forEach(i => { + console.log(i + " is " + k + " years old") + }) +}) + + + // TODO - Write for loop code here /* From be6cc79f94a59cd3174d14d273431d5c58b5e255 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 4 Mar 2023 11:24:00 +0000 Subject: [PATCH 06/14] Exercise E --- 1-exercises/E-while-loop-with-array/exercise.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/1-exercises/E-while-loop-with-array/exercise.js b/1-exercises/E-while-loop-with-array/exercise.js index d584cd75..a378041c 100644 --- a/1-exercises/E-while-loop-with-array/exercise.js +++ b/1-exercises/E-while-loop-with-array/exercise.js @@ -18,6 +18,12 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + for (let dateOfBirth of BIRTHDAYS) { + let getMonth = dateOfBirth.split(' ')[0]; + if (getMonth === "July") { + return dateOfBirth; + } + } } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" From 70c00b14598a1dd506046afd7aad974235752bda Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 4 Mar 2023 11:31:24 +0000 Subject: [PATCH 07/14] Weather Report --- 2-mandatory/1-weather-report.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..88595dda 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,12 @@ */ function getTemperatureReport(cities) { - // TODO + let report = []; + for (let city of cities) { + let temparatureOfCity = temperatureService(city); + report.push(`The temperature in ${city} is ${temparatureOfCity} degrees`); + } + return report; } From 316c43604e6a9736f0c066b1586396fef48dfb72 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 4 Mar 2023 12:07:03 +0000 Subject: [PATCH 08/14] Update 2-financial-times.js --- 2-mandatory/2-financial-times.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..30e33b87 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,7 +5,12 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + const shortArticleTitles = []; + for (let articleTitle of allArticleTitles) { + if (articleTitle.length <= 65) { + shortArticleTitles.push(articleTitle); + } + } } /* @@ -14,7 +19,16 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let fewestword = allArticleTitles[0].split(' ').length; + let result; + for (let articleTitle of allArticleTitles) { + let articleWords = articleTitle.split(' ').length; + if (articleWords < fewestword ) { + fewestword = articleWords; + result = articleTitle; + } + } + return result; } /* From f0e8f2b668d0372bfefe8451154e81b309e92f23 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 4 Mar 2023 16:00:32 +0000 Subject: [PATCH 09/14] Question 3 not completed yet --- 2-mandatory/2-financial-times.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 30e33b87..290a8a6c 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -46,6 +46,21 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + +let arrayOfLengths = allArticleTitles.map(w => w.length); +console.log(arrayOfLengths) +let sumOfLengths = 0; + for (let i = 0; i < arrayOfLengths.length; i++) { + sumOfLengths += arrayOfLengths[i]; + console.log(sumOfLengths) + } + +let sumOfArrayLengths = arrayOfLengths.length +console.log(sumOfArrayLengths) + +let result = sumOfLengths/sumOfArrayLengths; +console.log(result) + } From 616c53fa4d87a24db76fc61d2ebfe56118853351 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 11 Mar 2023 10:04:26 +0000 Subject: [PATCH 10/14] Update 2-financial-times.js --- 2-mandatory/2-financial-times.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 290a8a6c..cc96f558 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -37,7 +37,17 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + const regex = /[0-9]/; + let result = []; + for (let articleTitle of allArticleTitles) { + for (let i = 0; i < articleTitle.length; i++) { + if(articleTitle[i].match(regex)){ + result.push(articleTitle); + break; + } + } + } + return result; } /* From c65b895b7a0057bcf4334e03136a0680bda825cd Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sun, 12 Mar 2023 15:06:45 +0000 Subject: [PATCH 11/14] Fibonnacci --- 3-extra/3-fibonacci.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..b0d6bd3e 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,12 @@ */ function generateFibonacciSequence(n) { - // TODO + // declare the array starting with the first 2 values of the fibonacci sequence + // starting at array index 1, and push current index + previous index to the array + for (var fibonacci = [0, 1], i = 2; i < n; i++) +fibonacci.push(fibonacci[i-1] + fibonacci[i - 2]) + + return fibonacci } /* ======= TESTS - DO NOT MODIFY ===== */ From 8a908e8c108d0aff283ecac862ca18d0e6fdc8b6 Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Thu, 16 Mar 2023 12:17:12 +0000 Subject: [PATCH 12/14] Update 3-stocks.js Comment to show the stages of working - This needs to be put in a function --- 2-mandatory/3-stocks.js | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..386acc5e 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -8,6 +8,46 @@ */ /* ======= Stock data - DO NOT MODIFY ===== */ + +/* Thinking throught the steps + +let keys = STOCKS + +let arrayA = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[0] +let arrayM = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[1] +let arrayAZ = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[2] +let arrayG = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[3] +let arrayT = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[4] + + +let totalA = arrayA.reduce(function(a,b){ return +a + +b; }); +console.log(totalA) + +let totalM = arrayM.reduce(function(a,b){ return +a + +b; }); +console.log(totalM) + +let totalAZ = arrayAZ.reduce(function(a,b){ return +a + +b; }); +console.log(totalAZ) + +let totalG = arrayG.reduce(function(a,b){ return +a + +b; }); +console.log(totalG) + +let totalT = arrayT.reduce(function(a,b){ return +a + +b; }); +console.log(totalT) + + +totalA = Math.round(totalA).toFixed(2) +totalM = Math.round(totalM).toFixed(2) +totalAZ = Math.round(totalT).toFixed(2) +totalG = Math.round(totalG).toFixed(2) +totalT = Math.round(totalT).toFixed(2) + + + +let finalArray = [totalA/5, totalM/5, totalAZ/5, totalG/5, totalT/5] + +console.log(finalArray)*/ + const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"]; const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ @@ -18,6 +58,8 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ [1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA ]; +console.log(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS [0]) + /* We want to understand what the average price over the last 5 days for each stock is. Implement the below function, which From d1e8f3920fd2baaab1f466b7ef0881c48a87ca0e Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sat, 18 Mar 2023 09:47:24 +0000 Subject: [PATCH 13/14] Update 3-stocks.js --- 2-mandatory/3-stocks.js | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 386acc5e..a1bf85c0 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -77,6 +77,21 @@ console.log(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS [0]) */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let averageprice=[]; + + for(const stocksline of closingPricesForAllStocks){ + let sum = 0; + let average = 0; + for(const item of stocksline){ + sum = sum + item; + + } + average = sum / stocksline.length; + averageprice.push(Math.round(average * 100) / 100); + + } +return averageprice; + } /* @@ -91,6 +106,13 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + const priceChange = []; + for(const stocksline of closingPricesForAllStocks){ + let sub = 0; + sub = stocksline.slice(-1) - stocksline[0]; + priceChange.push(Math.round(sub*100)/100); + } + return priceChange; } /* @@ -107,6 +129,27 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + const result = []; +let num = 0; +let Name; +for(const stocksline of closingPricesForAllStocks){ + + + let FirstPrice = stocksline[0]; + let i = 1; + while(iFirstPrice){ + FirstPrice = price; + } + i=i+1; + } + + Name = stocks[num].toUpperCase(); + result.push(`The highest price of ${Name} in the last 5 days was ${FirstPrice.toFixed(2)}`); + num = num + 1; +} +return result; } From efc468dd2da7f51df9a3d17b2e749cc7512af04a Mon Sep 17 00:00:00 2001 From: Shahid Amin Date: Sun, 28 May 2023 10:56:25 +0100 Subject: [PATCH 14/14] extra exercise completed Functions for the radio station and the books completed. --- 3-extra/1-radio-stations.js | 14 ++++ 3-extra/2-array-of-objects.js | 131 ++++++++++++++++++---------------- 2 files changed, 83 insertions(+), 62 deletions(-) diff --git a/3-extra/1-radio-stations.js b/3-extra/1-radio-stations.js index 577076e9..ab1e0a9a 100644 --- a/3-extra/1-radio-stations.js +++ b/3-extra/1-radio-stations.js @@ -15,6 +15,14 @@ // `getAllFrequencies` goes here +function getAllFrequencies() { + const frequencies = []; + for (let i = 87; i <= 108; i++) { + frequencies.push(i); + } + return frequencies; +} + /** * Next, let's write a function that gives us only the frequencies that are radio stations. * Call this function `getStations`. @@ -26,6 +34,12 @@ */ // `getStations` goes here +function getStations() { + const frequencies = getAllFrequencies(); + const radioStations = frequencies.filter(isRadioStation); + return radioStations; +} + /* * ======= TESTS - DO NOT MODIFY ======= * Note: You are not expected to understand everything below this comment! diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..0468e5e4 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,72 +11,79 @@ */ function getHighestRatedInEachGenre(books) { - // TODO + // TODO + const genres = {}; + for (const book of books) { + if (!genres[book.genre] || book.rating > genres[book.genre].rating) { + genres[book.genre] = book; + } + } + const titles = Object.values(genres).map((book) => book.title); + return titles; } - /* ======= Book data - DO NOT MODIFY ===== */ const BOOKS = [ - { - title: "The Lion, the Witch and the Wardrobe", - genre: "children", - rating: 4.7 - }, - { - title: "Sapiens: A Brief History of Humankind", - genre: "non-fiction", - rating: 4.7 - }, - { - title: "Nadiya's Fast Flavours", - genre: "cooking", - rating: 4.7 - }, - { - title: "Harry Potter and the Philosopher's Stone", - genre: "children", - rating: 4.8 - }, - { - title: "A Life on Our Planet", - genre: "non-fiction", - rating: 4.8 - }, - { - title: "Dishoom: The first ever cookbook from the much-loved Indian restaurant", - genre: "cooking", - rating: 4.85 - }, - { - title: "Gangsta Granny Strikes Again!", - genre: "children", - rating: 4.9 - }, - { - title: "Diary of a Wimpy Kid", - genre: "children", - rating: 4.6 - }, - { - title: "BOSH!: Simple recipes. Unbelievable results. All plants.", - genre: "cooking", - rating: 4.6 - }, - { - title: "The Book Your Dog Wishes You Would Read", - genre: "non-fiction", - rating: 4.85 - }, -] - + { + title: "The Lion, the Witch and the Wardrobe", + genre: "children", + rating: 4.7, + }, + { + title: "Sapiens: A Brief History of Humankind", + genre: "non-fiction", + rating: 4.7, + }, + { + title: "Nadiya's Fast Flavours", + genre: "cooking", + rating: 4.7, + }, + { + title: "Harry Potter and the Philosopher's Stone", + genre: "children", + rating: 4.8, + }, + { + title: "A Life on Our Planet", + genre: "non-fiction", + rating: 4.8, + }, + { + title: + "Dishoom: The first ever cookbook from the much-loved Indian restaurant", + genre: "cooking", + rating: 4.85, + }, + { + title: "Gangsta Granny Strikes Again!", + genre: "children", + rating: 4.9, + }, + { + title: "Diary of a Wimpy Kid", + genre: "children", + rating: 4.6, + }, + { + title: "BOSH!: Simple recipes. Unbelievable results. All plants.", + genre: "cooking", + rating: 4.6, + }, + { + title: "The Book Your Dog Wishes You Would Read", + genre: "non-fiction", + rating: 4.85, + }, +]; /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the highest rated book in each genre", () => { - expect(new Set(getHighestRatedInEachGenre(BOOKS))).toEqual(new Set( - [ - "The Book Your Dog Wishes You Would Read", - "Gangsta Granny Strikes Again!", - "Dishoom: The first ever cookbook from the much-loved Indian restaurant" - ] - )); -}); \ No newline at end of file + expect(new Set(getHighestRatedInEachGenre(BOOKS))).toEqual( + new Set([ + "The Book Your Dog Wishes You Would Read", + "Gangsta Granny Strikes Again!", + "Dishoom: The first ever cookbook from the much-loved Indian restaurant", + ]) + ); +});