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. +*/ 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 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/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: 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()); +} 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); }); + diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..037210a9 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,8 +45,26 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let arr = []; + let total = 0; + closingPricesForAllStocks.forEach((element, i) => { + // console.log(element, element[i]); + total += element[i]; + + arr.push(total) + }); + + return arr; + } + + + /* + 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. Implement the below function, which 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", () => {