From 3ae4345c0a1e20f5c31e7793a48970c9afbdbcb3 Mon Sep 17 00:00:00 2001 From: khmdagal <81495872+khmdagal@users.noreply.github.com> Date: Wed, 21 Sep 2022 21:32:00 +0100 Subject: [PATCH 1/9] exercise A --- 1-exercises/A-undefined/exercise.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..e1a37460 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,25 +12,37 @@ // Example 1 let a; console.log(a); - +/*Answer +The code in example 1 will output undefined beasue a value has not been assigned to variable a, +So, when the program exacutes the code in line 14 will not get any output value. +*/ // Example 2 function sayHello() { - let message = "Hello"; + let message = "Hello"; } let hello = sayHello(); console.log(hello); +/* +The sayHello() function in example 2, is not return any value, therefore the program will print out undefined. +*/ // Example 3 function sayHelloToUser(user) { - console.log(`Hello ${user}`); + console.log(`Hello ${user}`); } sayHelloToUser(); +/* Answer for example 3 +The funciton is called with not value, to replace the 'user' parameter +*/ // Example 4 -let arr = [1,2,3]; +let arr = [1, 2, 3]; console.log(arr[3]); +/* +Index 3 is not defined means there is no value in index 3. Therefore, the program will output undefined message. +*/ From cfdc33a06803431c680bd0bbd986575935b4ed6c Mon Sep 17 00:00:00 2001 From: khmdagal <81495872+khmdagal@users.noreply.github.com> Date: Mon, 26 Sep 2022 18:04:17 +0100 Subject: [PATCH 2/9] exercise c --- 1-exercises/B-while-loop/exercise.js | 10 ++++- .../C-while-loop-with-array/exercise.js | 42 +++++++++++++++++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..02a7c609 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -1,12 +1,18 @@ /* while loops can be useful when you want to execute some code as long as some condition is true. - Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-seperated string. + Using a while loop, complete the function below so it logs (using console.log) + the first n even numbers as a comma-seperated string. The list of numbers should start with 0. n is being passed in as a parameter. */ function evenNumbers(n) { - // TODO + // TODO + let i = 0; + while (i % 2 === 0) { + console.log(); + i++; + } } evenNumbers(3); // should output 0,2,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..7c61f1fe 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -1,7 +1,9 @@ /* Loops can be useful when working with arrays. - In the below example, imagine we've defined an array holding the birthdays of your closest friends. - Use a while loop to search through the array until you find the first birthday in July, then return that birthday from the function. + In the below example, + imagine we've defined an array holding the birthdays of your closest friends. + Use a while loop to search through the array until you find the first birthday in July, + then return that birthday from the function. */ const BIRTHDAYS = [ @@ -16,8 +18,42 @@ const BIRTHDAYS = [ "November 15th" ]; +console.log(); + function findFirstJulyBDay(birthdays) { - // TODO + // TODO +// lets sort the array first a-z + + birthdays.sort(); + let i = 0; + while (i < birthdays.length) { + if (birthdays[i].includes('July')) { + return birthdays[i] + } + i++ + } + /* + using for loop + + + BIRTHDAYS.sort(); + for (let i = 0; i < birthdays.length; i++){ + if (birthdays[i].includes('July')) { + return birthdays[i] + } + + } + */ } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" + +/* +let i = 0; + while (birthdays[i] === 'July 11th') { + console.log(birthdays[i]) + i++; + } + + return birthdays[i] = birthdays[i]; +*/ \ No newline at end of file From b06fea079d7b37abd78577cbc455a56a71d6a38f Mon Sep 17 00:00:00 2001 From: khmdagal <81495872+khmdagal@users.noreply.github.com> Date: Mon, 26 Sep 2022 23:15:15 +0100 Subject: [PATCH 3/9] exercise E --- 1-exercises/E-for-loop/exercise1.js | 13 ++++++++----- 1-exercises/E-for-loop/exercise2.js | 4 +++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..492726bf 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -4,11 +4,14 @@ Change the while loop below into a for loop. */ - // Change the below code to use a for loop instead of a while loop. -let i = 0; -while(i < 26) { - console.log(String.fromCharCode(97 + i)); - i++; +// let i = 0; +// while(i < 26) { + +// i++; +// } + +for (let i = 0; i < 26; i++) { + console.log(String.fromCharCode(97 + 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..4ebbb832 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,7 +27,9 @@ const AGES = [ ]; // TODO - Write for loop code here - +for (let i = 0; i < WRITERS.length; i++){ + console.log(`${WRITERS[i]} ${AGES[i]} years old`); +} /* The output should look something like this: From 1658be7b900f960f953d58f99a9629e93dffdde2 Mon Sep 17 00:00:00 2001 From: khmdagal <81495872+khmdagal@users.noreply.github.com> Date: Mon, 26 Sep 2022 23:35:35 +0100 Subject: [PATCH 4/9] exercise f --- 1-exercises/D-do-while/exercise.js | 41 +++++++++++++++++++++++---- 1-exercises/F-for-of-loop/exercise.js | 17 +++++++---- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..db561bf3 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -1,15 +1,44 @@ /* - Sometimes when using loops, we'll want to execute the body of the loop at least once. We can make sure this happens by using a do-while loop. + Sometimes when using loops, we'll want to execute the body of the loop at least once. + We can make sure this happens by using a do-while loop. - If the condition in a while loop is initially false, the body of the loop will never execute - - But in a do-while loop, because the condition is checked after the body, we know that it will always execute at least once + - But in a do-while loop, because the condition is checked after the body, + we know that it will always execute at least once - Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0) + 1 Using a do-while loop, + 2 write a function which returns + *the sum of + *the first n even numbers (starting from 0) */ function evenNumbersSum(n) { // TODO + + let sum = 0; + let arr = []; + for (let i = 0; i < n; i++){ + if (i >0 && i % 2 === 0) { + arr.push(i); + }; + for (let j = 0; j < arr; j++) { + console.log(arr[j]); + } + // console.log(sum) + } + // return sum; } +evenNumbersSum(10); -console.log(evenNumbersSum(3)); // should output 6 -console.log(evenNumbersSum(0)); // should output 0 -console.log(evenNumbersSum(10)); // should output 90 \ No newline at end of file +/* + if (n[i] % 2 === 0) { + let arr = []; + arr.push(n[i]); + } +*/ +//evenNumbersSum(10); +// console.log(n) +/* +console.log(evenNumbersSum(3)); should output 6 +console.log(evenNumbersSum(0)); should output 0 + console.log(evenNumbersSum(10)); should output 90 +*/ diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..b3728915 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -4,13 +4,20 @@ // TODO Use a for-of loop to output each of the tube stations below. let tubeStations = [ - "Aldgate", - "Baker Street", - "Picadilly Circus", - "Oxford Street", - "Tottenham Court Road" + "Aldgate", + "Baker Street", + "Picadilly Circus", + "Oxford Street", + "Tottenham Court Road", ]; +for (let x of tubeStations) { + console.log(x); +} + // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (let i of str) { + console.log(i.toUpperCase()); +} From bd3d234a940f9a08cdbd8f894941b837fd8620b5 Mon Sep 17 00:00:00 2001 From: khmdagal <81495872+khmdagal@users.noreply.github.com> Date: Wed, 28 Sep 2022 14:28:07 +0100 Subject: [PATCH 5/9] mandatory 1,2 and 3 --- 2-mandatory/1-weather-report.js | 23 ++++++- 2-mandatory/2-retrying-random-numbers.js | 11 +++- 2-mandatory/3-financial-times.js | 84 +++++++++++++++++++----- 3 files changed, 100 insertions(+), 18 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..bb7c0ce1 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,9 +12,29 @@ */ function getTemperatureReport(cities) { - // TODO + // TODO + return cities.map(city => `The temperature in ${city} is ${temperatureService(city)} degrees`) + } +// console.log(getTemperatureReport(["London", "Paris", "São Paulo"])); + +/* + for (let city of cities) { + return `The temperature in ${temperatureService(city)} degrees` + } +cities.forEach((element, i) => { + console.log(element, i) +}); + */ +/* +getTemperatureReport([ +"London", +"Paris", +"São Paulo" +]) + */ + /* ======= TESTS - DO NOT MODIFY ===== */ @@ -31,6 +51,7 @@ function temperatureService(city) { return temparatureMap.get(city); } +// getTemperatureReport(cities); test("should return a temperature report for the user's cities", () => { let usersCities = [ diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..3ec34525 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -6,13 +6,22 @@ // This function shouldn't be changed function generateRandomNumber() { console.log("Generating number..."); - return Math.round(Math.random() * 100); + return Math.round(Math.random() * 100) ; } function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + +let i = 0; +do { + i = generateRandomNumber(); + + } while (i < 50); + + return i; } + /* ======= TESTS - DO NOT MODIFY ===== */ test("Returned value should always be greater than 50", () => { diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..0ec446f9 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -4,8 +4,24 @@ 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 + // TODO + // using filter method + + return allArticleTitles.filter((article) => article.length <= 65); + + /* + let pritableArticles = []; + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].length < 66) { + pritableArticles.push(allArticleTitles[i]); + } + } + + return pritableArticles; + + */ } /* @@ -14,7 +30,30 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + // TODO + + const wordCount = allArticleTitles.map( + (article) => article.split(" ").length + ); + + const theSmallest = Math.min(...wordCount); + + return allArticleTitles[wordCount.indexOf(theSmallest)]; + + /* +for (let i = 0; i < allArticleTitles.length; i++) { + console.log(allArticleTitles[i].split(" ").length); + } + +*/ + /* +let myArr = []; + for (let i = 0; i < allArticleTitles.length; i++) { + myArr.push(allArticleTitles[i].split(" ").length); + } + let leastWords = Math.min(...myArr); + return leastWords; +*/ } /* @@ -23,7 +62,10 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + // TODO + + return allArticleTitles.filter(article => /\d/.test(article)); + } /* @@ -31,24 +73,33 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + // TODO + let totalCharacters = 0; + for (let i = 0; i < allArticleTitles.length; i++) { + totalCharacters += allArticleTitles[i].length; + } + + return Math.floor(Math.round(totalCharacters / allArticleTitles.length)); } - - /* ======= 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", + "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", ]; +// console.log(averageNumberOfCharacters(ARTICLE_TITLES)); +// console.log(headlinesWithNumbers(ARTICLE_TITLES),"-----------"); +// console.log(titleWithFewestWords(ARTICLE_TITLES)); +// // console.log(headlinesWithNumbers(ARTICLE_TITLES)); +// console.log(potentialHeadlines(ARTICLE_TITLES), "using filter method"); /* ======= TESTS - DO NOT MODIFY ===== */ @@ -79,3 +130,4 @@ test("should only return headlines containing numbers", () => { test("should return the average number of characters in a headline", () => { expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65); }); + From b61902f89f346f97ba5f1edd79cc2e21fe02d482 Mon Sep 17 00:00:00 2001 From: khmdagal <81495872+khmdagal@users.noreply.github.com> Date: Wed, 28 Sep 2022 16:55:21 +0100 Subject: [PATCH 6/9] mandatory exercise 3 --- 2-mandatory/4-stocks.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..436750ae 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -18,6 +18,16 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ [1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA ]; +/* +let total = 0; + for (let i = 0; i < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS.length; i++) { + total += CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i]; + return total; + } +console.log(total); +*/ + + /* We want to understand what the average price over the last 5 days for each stock is. Implement the below function, which @@ -35,7 +45,26 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let total = 0; + + for (let i = 0; i < closingPricesForAllStocks.length; i++){ + console.log(closingPricesForAllStocks[i]); + for (let j = 0; j < closingPricesForAllStocks[i].length; j++){ + console.log(closingPricesForAllStocks[i][j]); + total += closingPricesForAllStocks[i][j] + + } + return total; + } + } + +console.log(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)); + + /* + 1) create for loop to go throught the outer array + 2) create inter for loop to go throught each element in the outer array + */ /* We also want to see what the change in price is from the first day to the last day for each stock. @@ -68,7 +97,7 @@ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { } -/* ======= TESTS - DO NOT MODIFY ===== */ +/* ======= 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] @@ -92,3 +121,4 @@ test("should return a description of the highest price for each stock", () => { ] ); }); +*/ \ No newline at end of file From 340bc4d819a5efd9378085609f373f898898c69c Mon Sep 17 00:00:00 2001 From: khmdagal <81495872+khmdagal@users.noreply.github.com> Date: Wed, 28 Sep 2022 22:04:21 +0100 Subject: [PATCH 7/9] try mandatory exercise 4 --- 2-mandatory/4-stocks.js | 46 +++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 436750ae..c55e7a18 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -45,21 +45,42 @@ console.log(total); */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let arr = []; let total = 0; - - for (let i = 0; i < closingPricesForAllStocks.length; i++){ - console.log(closingPricesForAllStocks[i]); - for (let j = 0; j < closingPricesForAllStocks[i].length; j++){ - console.log(closingPricesForAllStocks[i][j]); - total += closingPricesForAllStocks[i][j] + closingPricesForAllStocks.forEach((element, i) => { + // console.log(element, element[i]); + total += element[i]; - } - return total; - } + arr.push(total) + }); + + return arr; + + // for (let i = 0; i < closingPricesForAllStocks.length; i++){ + // console.log(closingPricesForAllStocks[i]) + // console.log(Math.a) + // } + +// for (let i = 0; i < closingPricesForAllStocks.length; i++){ + +// console.log(closingPricesForAllStocks[i], "-------i"); +// for (let j = 0; j < closingPricesForAllStocks[i].length; j++){ +// console.log(closingPricesForAllStocks[i][j],'------j'); + +// total += closingPricesForAllStocks[i][j]; + +// console.log(total,'----total') +// let average = total / closingPricesForAllStocks[i].length; +// arr.push(average.toFixed(2)); + +// } + +// } +// return arr } - -console.log(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)); + + /* 1) create for loop to go throught the outer array @@ -97,7 +118,7 @@ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { } -/* ======= TESTS - DO NOT MODIFY ===== +/* ======= 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] @@ -121,4 +142,3 @@ test("should return a description of the highest price for each stock", () => { ] ); }); -*/ \ No newline at end of file From db85f4805ed49c19d3fbbce20ab240e55b1e9da3 Mon Sep 17 00:00:00 2001 From: khmdagal <81495872+khmdagal@users.noreply.github.com> Date: Wed, 28 Sep 2022 22:05:37 +0100 Subject: [PATCH 8/9] Delete unwanted code --- 2-mandatory/4-stocks.js | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index c55e7a18..037210a9 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -56,28 +56,6 @@ function getAveragePrices(closingPricesForAllStocks) { return arr; - // for (let i = 0; i < closingPricesForAllStocks.length; i++){ - // console.log(closingPricesForAllStocks[i]) - // console.log(Math.a) - // } - - -// for (let i = 0; i < closingPricesForAllStocks.length; i++){ - -// console.log(closingPricesForAllStocks[i], "-------i"); -// for (let j = 0; j < closingPricesForAllStocks[i].length; j++){ -// console.log(closingPricesForAllStocks[i][j],'------j'); - -// total += closingPricesForAllStocks[i][j]; - -// console.log(total,'----total') -// let average = total / closingPricesForAllStocks[i].length; -// arr.push(average.toFixed(2)); - -// } - -// } -// return arr } From 29fea01adcdb48e81b8ab2798dbb5fdffb2b3335 Mon Sep 17 00:00:00 2001 From: khmdagal <81495872+khmdagal@users.noreply.github.com> Date: Sun, 9 Oct 2022 22:14:34 +0100 Subject: [PATCH 9/9] extra 1 --- 3-extra/1-factorial.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..dd1540b7 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,9 +9,19 @@ */ function factorial(input) { - // TODO + // Create new array to collect numbers + let arrayOfFactorial = []; + for (let i = 1; i < input + 1; i++){ + arrayOfFactorial.push(i) + } +// Used reduce method to multiply elements each other + const product = arrayOfFactorial.reduce((prevousValue, currentValue) => prevousValue * currentValue) + + return product; } + + /* ======= TESTS - DO NOT MODIFY ===== */ test("3! should be 6", () => {