From 017ff7bc4f64a5a66d1586da52a7a4ef85c0d0a6 Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Mon, 6 Mar 2023 00:28:44 +0000 Subject: [PATCH 1/6] Answered why the outputs in the exercises are undefined Answered using comments why the outputs in the exercises are undefined --- 1-exercises/A-undefined/exercise.js | 27 +++++++++++++++++++++++ 1-exercises/C-array-get-set/exercise.js | 5 +++-- 1-exercises/C-array-get-set/exercises2.js | 2 ++ 2-mandatory/1-weather-report.js | 3 ++- 2-mandatory/2-financial-times.js | 20 +++++++++++++++++ 5 files changed, 54 insertions(+), 3 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..240f947b 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -13,15 +13,28 @@ let a; console.log(a); +// The output of the example code console.log(a); will be undefined. +// We see undefined because the variable a has been declared but not assigned any value. +//In JavaScript, when a variable is declared but not assigned a value, its default value is undefined. +//So, when we log the value of a to the console, you will see undefined printed. + // Example 2 function sayHello() { let message = "Hello"; } + let hello = sayHello(); console.log(hello); +// The output in the example contains undefined because the sayHello function does not return any value explicitly. +// When a function is called, it can return a value using the return keyword. +// If the return keyword is not used in the function, the function will return undefined by default. +// In the example, the sayHello function prints "Hello" to the console but does not return any value. +// When the sayHello function is assigned to the result variable, the result variable is assigned the value of undefined because that is the value that the sayHello function returns by default. +// When the result variable is printed to the console, it will display undefined because that is the value that was assigned to it when the sayHello function was called. + // Example 3 function sayHelloToUser(user) { @@ -30,7 +43,21 @@ function sayHelloToUser(user) { sayHelloToUser(); +// The output contains undefined because the sayHelloToUser() function expects an argument user, but when it's called without any argument like sayHelloToUser(), the user parameter is undefined. +// The console.log() statement inside the function uses the user parameter to construct the message to be logged, so when user is undefined, the message will include undefined as the value of user. +// However, since the function doesn't have a return statement, its return value is undefined by default. +// Therefore, when you call sayHelloToUser() without any argument, it logs the message "Hello undefined" to the console, but the return value of the function is undefined. +// So the complete output of this code would be: +// undefined +// Hello undefined +// The first line is the message logged by the console.log() statement inside the sayHelloToUser() function, and the second line is the return value of the sayHelloToUser() function, which is undefined. + + // Example 4 let arr = [1,2,3]; console.log(arr[3]); + +// The output in the example below contains undefined because letters is an array with three elements, indexed from 0 to 2. +// When we try to access the element at index 3 (arr[3]), which is beyond the last index of the letters array, JavaScript returns undefined because there is no value at that index. + diff --git a/1-exercises/C-array-get-set/exercise.js b/1-exercises/C-array-get-set/exercise.js index 5ca911d5..dc284ca1 100644 --- a/1-exercises/C-array-get-set/exercise.js +++ b/1-exercises/C-array-get-set/exercise.js @@ -5,13 +5,14 @@ */ 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 } + /* DO NOT EDIT BELOW THIS LINE --------------------------- */ diff --git a/1-exercises/C-array-get-set/exercises2.js b/1-exercises/C-array-get-set/exercises2.js index 6b6b007a..3065412c 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 +let count = numbers.push(4); + /* DO NOT EDIT BELOW THIS LINE --------------------------- */ diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..ed65c209 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -11,7 +11,8 @@ - Hint: you can call the temperatureService function from your function */ -function getTemperatureReport(cities) { +function getTemperatureReport(cities, temparatureMap) { + // TODO } diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..baae68c1 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,6 +5,13 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { + let newArray = []; + for (let item of allArticleTitles) { + if (item.length <= 65) { + newArray.push(item); + } + } + return newArray; // TODO } @@ -14,7 +21,20 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { + let shortestHeadline; + let fewestNumberOfWords; + + for (let article of allArticleTitles) { + + } + + + // TODO + // loop through the headlines + // find number words in the headline + // find number of words in title + // compare the number with } /* From a5138d48ee4ccd1fd527c7efe7d0b5b377782978 Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Mon, 6 Mar 2023 00:42:41 +0000 Subject: [PATCH 2/6] Completed exercise tasks B and C --- 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..5bec732c 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 de3fc86ff8b0f9185521f89fb9c28273c4f96a6b Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Tue, 7 Mar 2023 22:43:43 +0000 Subject: [PATCH 3/6] Completed Exercises D and E and Mandatory task 1 and task 2 --- 1-exercises/D-for-loop/exercise.js | 8 ++++ .../E-while-loop-with-array/exercise.js | 8 +++- 2-mandatory/1-weather-report.js | 9 +++- 2-mandatory/2-financial-times.js | 47 ++++++++++++++++--- 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/1-exercises/D-for-loop/exercise.js b/1-exercises/D-for-loop/exercise.js index 081002b2..4a41b680 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.js @@ -28,6 +28,10 @@ const AGES = [ // TODO - Write for loop code here +for (i = 0; i < WRITERS.length; i++) { + console.log(`${WRITERS[i]} is ${AGES[i]} years old`); +} + /* The output should look something like this: @@ -37,3 +41,7 @@ Jane Austen is 41 years old Bell Hooks is 63 years old Yukiko Motoya is 49 years old */ + + + + diff --git a/1-exercises/E-while-loop-with-array/exercise.js b/1-exercises/E-while-loop-with-array/exercise.js index d584cd75..3714d034 100644 --- a/1-exercises/E-while-loop-with-array/exercise.js +++ b/1-exercises/E-while-loop-with-array/exercise.js @@ -16,8 +16,14 @@ const BIRTHDAYS = [ "November 15th" ]; -function findFirstJulyBDay(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" diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index ed65c209..c86cef8a 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -11,8 +11,13 @@ - Hint: you can call the temperatureService function from your function */ -function getTemperatureReport(cities, temparatureMap) { - +function getTemperatureReport(cities) { + let arr = []; + for (let city of cities) { + let weatherReport = `The temperature in ${city} is ${temperatureService(city)} degrees`; + arr.push(weatherReport); + } + return arr; // TODO } diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index baae68c1..5cf02301 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -20,16 +20,21 @@ function potentialHeadlines(allArticleTitles) { Implement the function below, which returns the title with the fewest words. (you can assume words will always be seperated by a space) */ +// ["The", "three", "questions", "that", "dominate", "investment"] +// fewestNumberOfWords = articleSplitter.length; + function titleWithFewestWords(allArticleTitles) { let shortestHeadline; - let fewestNumberOfWords; - - for (let article of allArticleTitles) { + let fewestNumberOfWords = 1000; + for (let headline of allArticleTitles) { + let splitArticleTitle = (headline.split(" ")); + if (splitArticleTitle.length < fewestNumberOfWords) { + fewestNumberOfWords = splitArticleTitle.length; + shortestHeadline = headline; + } } - - - + return shortestHeadline; // TODO // loop through the headlines // find number words in the headline @@ -37,21 +42,50 @@ function titleWithFewestWords(allArticleTitles) { // compare the number with } + /* 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) */ + +// 1. We are getting array of string. We need to find if the string includes numbers. +// 2. Once we find a number, we will put the string to the new array. +// 3. We are going to return the new array. New array is string with numbers. + function headlinesWithNumbers(allArticleTitles) { + let newArrayWithNum = []; + for (const title of allArticleTitles) { + if (/\d/.test(title)) { + newArrayWithNum.push(title); + } + } + + return newArrayWithNum; // TODO } + // for (character of title) { + // if (character.includes(Number)) { + // newArrayWithNum.push(title) + // } + /* 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. */ +// 1. We find total characters +// 2. We need to find the number of Article titles. +// 3. We divide (total character) by (number of article titles). +// 4. Round it up + function averageNumberOfCharacters(allArticleTitles) { // TODO + let totalCharacter = 0; + for (let item of allArticleTitles) { + totalCharacter = totalCharacter + item.length; + } + return Math.round(totalCharacter / allArticleTitles.length); } @@ -70,6 +104,7 @@ const ARTICLE_TITLES = [ "Brussels urges Chile's incoming president to endorse EU trade deal", ]; + /* ======= TESTS - DO NOT MODIFY ===== */ test("should only return potential headlines", () => { From e32cf5fa131923b9f845bd343de653863a02f59d Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Wed, 8 Mar 2023 22:50:10 +0000 Subject: [PATCH 4/6] Completed the rest of the mandatory tasks --- 2-mandatory/3-stocks.js | 60 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..201f200d 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -33,8 +33,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! */ + +// 1. Find the total price of each company stocks in the last 5 days +// 2. Divide the total price by the number of days i.e. 5 + + + function getAveragePrices(closingPricesForAllStocks) { - // TODO + let arrayOfAverageValues = []; + for (let closingPriceEachCompany of closingPricesForAllStocks) { + let sumOfClosingPriceEachCompany = 0; + for (i = 0; i < closingPriceEachCompany.length; i++) { + sumOfClosingPriceEachCompany = sumOfClosingPriceEachCompany + closingPriceEachCompany[i]; + averagePriceEachCompany = (sumOfClosingPriceEachCompany / closingPriceEachCompany.length).toFixed(2); + } + averagePricesNumberType = parseFloat(averagePriceEachCompany); + arrayOfAverageValues.push(averagePricesNumberType); + } + return arrayOfAverageValues; } /* @@ -48,7 +64,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 newPriceChangeArray = []; + for (let closingPricesEachcompany of closingPricesForAllStocks) { + let priceChange = (closingPricesEachcompany[4] - closingPricesEachcompany[0]).toFixed(2); + let priceChangeValues = parseFloat(priceChange); + newPriceChangeArray.push(priceChangeValues); + } + return newPriceChangeArray; } /* @@ -63,9 +85,35 @@ function getPriceChanges(closingPricesForAllStocks) { The stock ticker should be capitalised. The price should be shown with exactly 2 decimal places. */ -function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO -} + + // for (i = 0; i < closingPricesForAllStocks.length; i++) { + // let highestPrice = 0; + // for (let companyPriceArray of closingPricesForAllStocks) { + // if (companyPriceArray[i] > highestPrice) { + // highestPrice = companyPriceArray[i]; + // } + // } + // arrayWithHighestValue.push(highestPrice); + + function highestPriceDescriptions(closingPricesForAllStocks, stocks) { + let highestCompanyStockPrices = []; + let namesOfCompanies = []; + let highestPrice = 0; + for (i = 0; i < closingPricesForAllStocks.length; i++) { + for (let closingPricePerDay of closingPricesForAllStocks[i]) { + if (closingPricePerDay > highestPrice) { + highestPrice = closingPricePerDay; + } + } + let upperStock = stocks[i].toUpperCase(); + highestCompanyStockPrices.push( + `The highest price of ${upperStock} in the last 5 days was ${highestPrice.toFixed(2)}`); + highestPrice = 0; + } + return highestCompanyStockPrices; + // return `The highest price of ${stocks} in the last 5 days was ${arrayWithHighestValue[i]}`; + } + /* ======= TESTS - DO NOT MODIFY ===== */ @@ -81,7 +129,7 @@ test("should return the price change for each stock", () => { ); }); -test("should return a description of the highest price for each stock", () => { +test.only("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", From 0c6af51acc06ae43c120ba19c861987e51abec7a Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Wed, 22 Mar 2023 00:20:26 +0000 Subject: [PATCH 5/6] Made some changes to exercises, mandatory and tried the extra part --- 1-exercises/A-undefined/exercise.js | 4 ++-- .../E-while-loop-with-array/exercise.js | 5 ++-- 2-mandatory/1-weather-report.js | 6 +++++ 2-mandatory/3-stocks.js | 24 +++++++++++++------ 3-extra/2-array-of-objects.js | 9 +++++++ 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 240f947b..896d95bf 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -15,8 +15,8 @@ console.log(a); // The output of the example code console.log(a); will be undefined. // We see undefined because the variable a has been declared but not assigned any value. -//In JavaScript, when a variable is declared but not assigned a value, its default value is undefined. -//So, when we log the value of a to the console, you will see undefined printed. +// In JavaScript, when a variable is declared but not assigned a value, its default value is undefined. +// So, when we log the value of a to the console, you will see undefined printed. // Example 2 diff --git a/1-exercises/E-while-loop-with-array/exercise.js b/1-exercises/E-while-loop-with-array/exercise.js index 3714d034..3e4370b9 100644 --- a/1-exercises/E-while-loop-with-array/exercise.js +++ b/1-exercises/E-while-loop-with-array/exercise.js @@ -20,8 +20,9 @@ function findFirstJulyBDay(BIRTHDAYS) { // TODO let i = 0; while (i < BIRTHDAYS.length) { - if (BIRTHDAYS[i].startsWith("July")) - return BIRTHDAYS[i]; + if (BIRTHDAYS[i].startsWith("July")) { + return BIRTHDAYS[i]; + } i++; } } diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index c86cef8a..b469528f 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -11,6 +11,12 @@ - Hint: you can call the temperatureService function from your function */ +// 1. Create a new empty array +// 2. Loop through the cities, and for each city: +// a. Create a new string for the weather report of that city +// b. Add that new string into the array +// 3. Return the array + function getTemperatureReport(cities) { let arr = []; for (let city of cities) { diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 201f200d..182eb811 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -63,12 +63,16 @@ function getAveragePrices(closingPricesForAllStocks) { (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) */ + +// 1. AAPL (apple) price difference last day - first day = -6.2 +// + function getPriceChanges(closingPricesForAllStocks) { let newPriceChangeArray = []; - for (let closingPricesEachcompany of closingPricesForAllStocks) { - let priceChange = (closingPricesEachcompany[4] - closingPricesEachcompany[0]).toFixed(2); - let priceChangeValues = parseFloat(priceChange); - newPriceChangeArray.push(priceChangeValues); + for (let oneSetOfPrices of closingPricesForAllStocks) { + let priceChange = (oneSetOfPrices[(oneSetOfPrices.length - 1)] - oneSetOfPrices[0]).toFixed(2); //converted to 2 decimal places but it is also converted to string in the process + let priceChangeValues = parseFloat(priceChange); // removing the string values to numbers + newPriceChangeArray.push(priceChangeValues); } return newPriceChangeArray; } @@ -87,7 +91,13 @@ function getPriceChanges(closingPricesForAllStocks) { */ // for (i = 0; i < closingPricesForAllStocks.length; i++) { - // let highestPrice = 0; + // let highestPrice = 0;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 + // for (let companyPriceArray of closingPricesForAllStocks) { // if (companyPriceArray[i] > highestPrice) { // highestPrice = companyPriceArray[i]; @@ -123,13 +133,13 @@ test("should return the average price for each stock", () => { ); }); -test("should return the price change for each stock", () => { +test.only("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.only("should return a description of the highest price for each stock", () => { +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", diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..4d598a37 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -10,6 +10,15 @@ Each title in the resulting array should be the highest rated book in its genre. */ +// note: Steps to create a function. think about argument. think about input and output and its types. array of object mean array of what type or what type of array. +// blue word before the colon is key or value or property +// we can use teh square bracket +// const books = [{title: "The Lion King"}] // of an array +// const book = {title; "The Lion King"} +// 2 ways to access the value: +// book.title // "The Lion King" +// book["tile"] //"The Lion King" + function getHighestRatedInEachGenre(books) { // TODO } From c1d359dfae2de7007fd380cedd13de95db2c7c3a Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Wed, 22 Mar 2023 00:41:10 +0000 Subject: [PATCH 6/6] Created a small test in weather report task We checked if we have an array that is the same length as the number of cities passed in. --- 2-mandatory/1-weather-report.js | 5 +++++ 2-mandatory/3-stocks.js | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index b469528f..4b88fbe1 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -44,6 +44,11 @@ function temperatureService(city) { return temparatureMap.get(city); } +test("should return array of same length as argument", () => { + let usersCities = ["London", "Paris", "São Paulo"]; + expect(getTemperatureReport(usersCities).length).toEqual(3); +}); + test("should return a temperature report for the user's cities", () => { let usersCities = [ "London", diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 182eb811..dd5ef167 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -133,7 +133,7 @@ test("should return the average price for each stock", () => { ); }); -test.only("should return the price change for each stock", () => { +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] );