diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..ff2cc0ee 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,16 +12,18 @@ // Example 1 let a; console.log(a); - +// a has not a value that's why undefined. // Example 2 function sayHello() { let message = "Hello"; } +// function has not a return part. + let hello = sayHello(); console.log(hello); - +// There is nothing in the paranthesis of sayHello. // Example 3 function sayHelloToUser(user) { @@ -29,8 +31,11 @@ function sayHelloToUser(user) { } sayHelloToUser(); - +// function call has no argument // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// There is not any element at the index(3). Array index ends at 2. + +//No questions \ No newline at end of file diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..8c7bb1f6 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,7 +6,16 @@ */ function evenNumbers(n) { - // TODO + const even = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]; + + let string = ""; + let count = 0; + while (count < n) { + string += `${even[count]},`; + count++; + } + + console.log(string); } 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..ed7836dc 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -5,19 +5,25 @@ */ const BIRTHDAYS = [ - "January 7th", - "February 12th", - "April 3rd", - "April 5th", - "May 3rd", - "July 11th", - "July 17th", - "September 28th", - "November 15th" + "January 7th", + "February 12th", + "April 3rd", + "April 5th", + "May 3rd", + "July 11th", + "July 17th", + "September 28th", + "November 15th", ]; function findFirstJulyBDay(birthdays) { - // TODO + let index = 0; + while (index < birthdays.length) { + if (birthdays[index].includes("July")) return birthdays[index]; + index++; + } } - console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" + + +//Ask about upper-lowercase. \ 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..f6ede27a 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,17 @@ */ function evenNumbersSum(n) { - // TODO + const even = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]; + + let string = ""; + let counter= 0; + + do { + string += `${even[counter]},`; + counter++; + } while (counter < n); + + return string; } console.log(evenNumbersSum(3)); // should output 6 diff --git a/1-exercises/E-for-loop/exercise1.js b/1-exercises/E-for-loop/exercise1.js index db5fac64..5be10eb0 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,9 @@ // 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..999abee0 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -26,8 +26,9 @@ const AGES = [ 49 ]; -// TODO - Write for loop code here - +for (let i = 0; i < WRITERS.length; i++) { + console.log(WRITERS[i] + " is "+ 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..1f42cd97 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -4,13 +4,19 @@ // 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 (const element of tubeStations) { + console.log(element); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for (let char of str) { + console.log(char.toUpperCase()); +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..95f82c43 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,52 +12,49 @@ */ function getTemperatureReport(cities) { - // TODO + let arr = []; + for (let element of cities) { + let temp = temperatureService(element); + arr.push(`The temperature in ${element} is ${temp} degrees`); + } + return arr; } - /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { - let temparatureMap = new Map(); - - temparatureMap.set('London', 10); - temparatureMap.set('Paris', 12); - temparatureMap.set('Barcelona', 17); - temparatureMap.set('Dubai', 27); - temparatureMap.set('Mumbai', 29); - temparatureMap.set('São Paulo', 23); - temparatureMap.set('Lagos', 33); - - return temparatureMap.get(city); + let temparatureMap = new Map(); + + temparatureMap.set("London", 10); + temparatureMap.set("Paris", 12); + temparatureMap.set("Barcelona", 17); + temparatureMap.set("Dubai", 27); + temparatureMap.set("Mumbai", 29); + temparatureMap.set("São Paulo", 23); + temparatureMap.set("Lagos", 33); + + return temparatureMap.get(city); } test("should return a temperature report for the user's cities", () => { - let usersCities = [ - "London", - "Paris", - "São Paulo" - ] - - expect(getTemperatureReport(usersCities)).toEqual([ - "The temperature in London is 10 degrees", - "The temperature in Paris is 12 degrees", - "The temperature in São Paulo is 23 degrees" - ]); + let usersCities = ["London", "Paris", "São Paulo"]; + + expect(getTemperatureReport(usersCities)).toEqual([ + "The temperature in London is 10 degrees", + "The temperature in Paris is 12 degrees", + "The temperature in São Paulo is 23 degrees", + ]); }); test("should return a temperature report for the user's cities (alternate input)", () => { - let usersCities = [ - "Barcelona", - "Dubai" - ] - - expect(getTemperatureReport(usersCities)).toEqual([ - "The temperature in Barcelona is 17 degrees", - "The temperature in Dubai is 27 degrees" - ]); + let usersCities = ["Barcelona", "Dubai"]; + + expect(getTemperatureReport(usersCities)).toEqual([ + "The temperature in Barcelona is 17 degrees", + "The temperature in Dubai is 27 degrees", + ]); }); test("should return an empty array if the user hasn't selected any cities", () => { - expect(getTemperatureReport([])).toEqual([]); -}); \ No newline at end of file + expect(getTemperatureReport([])).toEqual([]); +}); diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..be3d3d24 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,7 +10,12 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let call = 0; + do { + call = generateRandomNumber(); + } while (call <= 50); + + return call; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..dd3773f8 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,16 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + for (let i of closingPricesForAllStocks) { + let total = 0; + for (let j in i) { + total += i[j]; + } + let a = total / i.length; + let b = Number(a.toFixed(2)); + arrayOfAverage.push(b); + } + return arrayOfAverage; } /* @@ -48,7 +57,12 @@ 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 array of closingPricesForAllStocks) { + let output = array[array.length - 1] - array[0]; + priceChanges.push(Number(output.toFixed(2))); + } + return priceChanges; } /* @@ -64,7 +78,15 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let returnMyArray = []; + for (let index in closingPricesForAllStocks) { + let largestNumber = Math.max(...closingPricesForAllStocks[index]); + let outputWithQuote = `The highest price of ${stocks[ + index + ].toUpperCase()} in the last 5 days was ${largestNumber.toFixed(2)}`; + returnMyArray.push(outputWithQuote); + } + return returnMyArray; } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..a8d069e0 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,7 +9,10 @@ */ function factorial(input) { - // TODO + for (var i = input - 1; i >= 1; i--) { + input *= i; + } + return input; } /* ======= TESTS - DO NOT MODIFY ===== */