From aed52343ed683ee1043e2b8d56453b30f913ed31 Mon Sep 17 00:00:00 2001 From: Farnooshmo <91014204+Farnooshmo@users.noreply.github.com> Date: Wed, 14 Dec 2022 20:23:42 +0000 Subject: [PATCH 01/12] Exercises A & B completed. --- 1-exercises/A-undefined/exercise.js | 8 ++++---- 1-exercises/B-while-loop/exercise.js | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..eca4e074 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -10,13 +10,13 @@ */ // Example 1 -let a; +let a; // The a variable has declared and need to be assigned to a value. let a = 12; or let a = "Hello"; console.log(a); // Example 2 function sayHello() { - let message = "Hello"; + let message = "Hello"; // The function doesn't have return, therefore it is undefined. } let hello = sayHello(); @@ -28,9 +28,9 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); // There is no value for the parameter // Example 4 let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]); //The arr[3] refers to an item that does not exist. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..8da98794 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,7 +6,13 @@ */ function evenNumbers(n) { - // TODO + let arr = []; + let i = 0; + while(i < n) { + arr.push(i * 2); + i++; + } + console.log(arr.join()); } evenNumbers(3); // should output 0,2,4 From bec25340eef0c5777c39c2d5494a04b416da2cfa Mon Sep 17 00:00:00 2001 From: Farnooshmo <91014204+Farnooshmo@users.noreply.github.com> Date: Wed, 14 Dec 2022 23:36:57 +0000 Subject: [PATCH 02/12] Exercises/c-while-loop-with-array completed --- 1-exercises/A-undefined/exercise.js | 2 +- 1-exercises/C-while-loop-with-array/exercise.js | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index eca4e074..1c77ccb9 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -28,7 +28,7 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); // There is no value for the parameter +sayHelloToUser(); // There is no value for the parameter, user is undefined. // Example 4 diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..a0bd5275 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,15 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + + let i = 0; + while(i < BIRTHDAYS.length){ + if(BIRTHDAYS[i].startsWith("July"){ + return BIRTHDAYS[i]; + } + i++; + } + } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" From 0fae195920a9b1500ebca885f16ad49e406129c6 Mon Sep 17 00:00:00 2001 From: Farnooshmo <91014204+Farnooshmo@users.noreply.github.com> Date: Wed, 14 Dec 2022 23:54:49 +0000 Subject: [PATCH 03/12] Exercises/D-do-while/ completed --- 1-exercises/D-do-while/exercise.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..08a5793e 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 i = 0; + let sum = 0; + do { + sum += (i * 2); + i++; + } + while(n > i); + return sum; } console.log(evenNumbersSum(3)); // should output 6 From 4132e59dbe3660be657aeafe88a244d92a172e5c Mon Sep 17 00:00:00 2001 From: Farnooshmo <91014204+Farnooshmo@users.noreply.github.com> Date: Thu, 15 Dec 2022 00:11:30 +0000 Subject: [PATCH 04/12] E-for-loop completed --- 1-exercises/E-for-loop/exercise1.js | 5 ++--- 1-exercises/E-for-loop/exercise2.js | 5 ++++- 2 files changed, 6 insertions(+), 4 deletions(-) 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..c59385b8 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -26,7 +26,10 @@ const AGES = [ 49 ]; -// TODO - Write for loop code here +for(i = 0; i < WRITERS.length; i++){ + let ageOfWriters = WRITERS[i] + " is " + AGES[i] + " years old"; + console.log(ageOfWriters); +} /* The output should look something like this: From 3537ba908c12981c252e250c1b67acfa1710646b Mon Sep 17 00:00:00 2001 From: Farnooshmo <91014204+Farnooshmo@users.noreply.github.com> Date: Thu, 15 Dec 2022 00:16:53 +0000 Subject: [PATCH 05/12] F-for-of-loop completed --- 1-exercises/F-for-of-loop/exercise.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..b0abc1f3 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -10,7 +10,12 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; - +for( let stations of tubeStations){ + console.log(stations); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for( let letter of str){ + console.log(letter.toUpperCase()); +} From 942d40df05e1a517628c51c8bce253f37a86920c Mon Sep 17 00:00:00 2001 From: Farnooshmo <91014204+Farnooshmo@users.noreply.github.com> Date: Thu, 15 Dec 2022 00:32:39 +0000 Subject: [PATCH 06/12] Mandatory/1-weather- report completed --- 2-mandatory/1-weather-report.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..d8d3e761 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,13 @@ */ function getTemperatureReport(cities) { - // TODO + + let temperatureOfCity = []; + for(let city of cities){ + let temperature = temperatureService(city); + temperatureOfCity.push('The temperature in '+ city + ' is '+ temperature +' degrees'); + } + return temperatureOfCity; } From 55ba43eef4fada0a8ae3d07e52d6612c336812dd Mon Sep 17 00:00:00 2001 From: Farnooshmo <91014204+Farnooshmo@users.noreply.github.com> Date: Thu, 15 Dec 2022 23:28:53 +0100 Subject: [PATCH 07/12] correct the mistake --- 1-exercises/C-while-loop-with-array/exercise.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index a0bd5275..7f879da1 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -20,7 +20,7 @@ function findFirstJulyBDay(birthdays) { let i = 0; while(i < BIRTHDAYS.length){ - if(BIRTHDAYS[i].startsWith("July"){ + if(BIRTHDAYS[i].startsWith("July")){ return BIRTHDAYS[i]; } i++; From 8848341b0b4e498a2391a98a37647570c0e74f62 Mon Sep 17 00:00:00 2001 From: Farnooshmo <91014204+Farnooshmo@users.noreply.github.com> Date: Thu, 15 Dec 2022 23:41:44 +0100 Subject: [PATCH 08/12] Mandatory 2 completed --- 2-mandatory/2-retrying-random-numbers.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..10f89296 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,7 +10,13 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let number = 0; + + do{ + number = generateRandomNumber(); + } + while (number <= 50) + return number; } /* ======= TESTS - DO NOT MODIFY ===== */ From f0e45e38542ca3edb3674fe13756608412bb0ecc Mon Sep 17 00:00:00 2001 From: Farnooshmo <91014204+Farnooshmo@users.noreply.github.com> Date: Fri, 16 Dec 2022 01:26:49 +0100 Subject: [PATCH 09/12] Mandatory 3 completed --- 2-mandatory/3-financial-times.js | 38 +++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..d7032e98 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,16 +5,30 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + newArticleArray =[]; + for(let title of allArticleTitles){ + if(title.length <= 65){ + newArticleArray.push(title); + } + } + return newArticleArray; } - /* 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 + let shortTitle = 0; + let i = 0; + for(let allArticle of allArticleTitles){ + let words = allArticle.split(" ").length; + if(shortTitle > words){ + shortTitle = words; + titleWithFewestWords = all; + } + } + return titleWithFewestWords; } /* @@ -23,15 +37,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 -} + let newArrayOfHeadlines = []; + for(let allTitles of allArticleTitles){ + if(allTitles.match(/\d/)){ + newArrayOfHeadlines.push(allTitles); + } + } + return newArrayOfHeadlines; + } + /* 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 numberOfCharacters = 0; + for(let article of allArticleTitles){ + numberOfCharacters += article.length; + } + let averageCharacters = Math.round(numberOfCharacters / allArticleTitles.length); + return averageCharacters; } From 11553b168ee8429dfa2f7362ee90f71e7c00dfa1 Mon Sep 17 00:00:00 2001 From: Farnooshmo <91014204+Farnooshmo@users.noreply.github.com> Date: Thu, 29 Dec 2022 18:31:49 +0000 Subject: [PATCH 10/12] Mandatory & extra completed. --- 2-mandatory/3-financial-times.js | 13 +++++-------- 2-mandatory/4-stocks.js | 31 ++++++++++++++++++++++++++++--- 3-extra/1-factorial.js | 6 +++++- 3-extra/2-array-of-objects.js | 9 ++++++++- 3-extra/3-fibonacci.js | 8 +++++++- README.md | 2 +- 6 files changed, 54 insertions(+), 15 deletions(-) diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index d7032e98..04b7bb36 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -19,16 +19,13 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - let shortTitle = 0; - let i = 0; - for(let allArticle of allArticleTitles){ - let words = allArticle.split(" ").length; - if(shortTitle > words){ - shortTitle = words; - titleWithFewestWords = all; + let shortestTitle = allArticleTitles[0]; + for(let title of allArticleTitles){ + if(title.split(" ").length < shortestTitle.split(" ").length){ + shortestTitle = title; } } - return titleWithFewestWords; + return shortestTitle; } /* diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..ff81ebd1 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -7,6 +7,7 @@ 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"]; @@ -34,7 +35,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + + let averagePrices = []; + for( let closingPrices of closingPricesForAllStocks){ + let sum = 0; + for(let price of closingPrices){ + sum += price; + } + let averagePrice = sum / closingPrices.length; + averagePrice Math.round(averagePrice * 100)/100; + averagePrices.push(averagePrice); + } + return averagePrices; } /* @@ -48,7 +60,13 @@ function getAveragePrices(closingPricesForAllStocks) { The price change value should be rounded to 2 decimal places, and should be a number (not a string) */ function getPriceChanges(closingPricesForAllStocks) { - // TODO + let priceChanges = []; + for(let closingPrices of closingPricesForAllStocks){ + let priceChange = closingPrices[closingPrices.length - 1] -closingPrices[0]; + priceChange = Math.round(priceChange * 100)/100; + priceChanges.push(priceChange); + } +return priceChanges } /* @@ -64,7 +82,14 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let descriptions = []; + for(let i = 0; i < closingPricesForAllStocks.length; i++){ + let highestPrice = Math.max(...closingPricesForAllStocks[i]); + let stockTicker = stocks[i].toUpperCase(); + let description = `The highest price of ${stockTicker} in the last 5 days was ${highestPrice.toFixed(2)}`; + descriptions.push(description); + } + return descriptions; } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..14eddaf5 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,7 +9,11 @@ */ function factorial(input) { - // TODO + let result = 1; + for(let i = input; i > 0; i--){ + result *= i; + } +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..efc81a41 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,7 +11,14 @@ */ function getHighestRatedInEachGenre(books) { - // TODO + const highestRated = []; + for(let book of books){ + const {title, genre, rating} = book; + if(!highestRated[genre] || rating > highestRated[genre].rating){ + highestRated[genre] = {title, rating}; + } + } + return Object.values(highestRated).map((book) => book.title); } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..ba0a735d 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,13 @@ */ function generateFibonacciSequence(n) { - // TODO + + let sequence = [0, 1]; + for(let i = 2; i < n; i++){ + let nextNumber = sequence[i - 1] + sequence[i - 2]; + sequence.push(nextNumber); + } + return sequence } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/README.md b/README.md index 1ca17aae..0355d3de 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ This is a **private** repository. Please request access from your Teachers, Budd ## Testing your work -- Each of the *.js files in the `1-exercises` folder can be run from the terminal using the `node` command with the path to the file. For example, `node 1-exercises/A-undefined/exercise.js` can be run from the root of the project. +- Each of the *.js files in the `1-exercises` folder can be run from the terminal using the `node` command with the path to the file. For example, node 1-exercises/A-undefined/exercise.js`` can be run from the root of the project. - To run the tests in the `2-mandatory` folder, run `npm run test` from the root of the project (after having run `npm install` once before). - To run the tests in the `3-extra` folder, run `npm run extra-tests` from the root of the project (after having run `npm install` once before). From a58439b63c40f92af72290b25197e3701782e7ef Mon Sep 17 00:00:00 2001 From: Farnoosh <91014204+Farnooshmo@users.noreply.github.com> Date: Fri, 6 Jan 2023 10:00:21 +0000 Subject: [PATCH 11/12] Update package.json --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index dc4e9ace..77c1ca53 100644 --- a/package.json +++ b/package.json @@ -23,5 +23,8 @@ "homepage": "https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week3#readme", "devDependencies": { "jest": "^26.6.3" + }, + "dependencies": { + "git-it": "^1.2.4" } } From 1c462cba75809a79c5747755de14d87a77a56793 Mon Sep 17 00:00:00 2001 From: Farnoosh <91014204+Farnooshmo@users.noreply.github.com> Date: Tue, 10 Jan 2023 21:18:57 +0000 Subject: [PATCH 12/12] update 4-stocks.js --- 2-mandatory/4-stocks.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index ff81ebd1..50cdb57b 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,20 +34,24 @@ 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) { - + +function getAverage(closingPrices){ + let sum = 0; + for(let i = 0; i < closingPrices.length; i++){ + sum += closingPrices[i]; + } + return sum / closingPrices.length; +}; + +function getAveragePrices(closingPricesForAllStocks){ let averagePrices = []; - for( let closingPrices of closingPricesForAllStocks){ - let sum = 0; - for(let price of closingPrices){ - sum += price; - } - let averagePrice = sum / closingPrices.length; - averagePrice Math.round(averagePrice * 100)/100; + for(let closingPrices of closingPricesForAllStocks){ + let averagePrice = getAverage(closingPrices); + averagePrice = Math.round(averagePrice * 100)/100; averagePrices.push(averagePrice); } return averagePrices; -} +}; /* We also want to see what the change in price is from the first day to the last day for each stock.