diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..d3fee658 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -9,10 +9,10 @@ For each example, can you explain why we are seeing undefined? */ -// Example 1 +// Example 1 let a; console.log(a); - +// Answer: Its because there is no value for a. // Example 2 function sayHello() { @@ -21,7 +21,7 @@ function sayHello() { let hello = sayHello(); console.log(hello); - +// Answer: We didn't call the variable message so it won't output it. // Example 3 function sayHelloToUser(user) { @@ -29,8 +29,9 @@ function sayHelloToUser(user) { } sayHelloToUser(); - +// Answer:Because user is undefined // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// Answer: We only have 0,1,2 index and there is no value for index 3. \ 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..97a7f02d 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,6 +7,19 @@ function evenNumbers(n) { // TODO + + let numbers = []; + + let i = 0; + + while (i < n) { + + numbers.push(i * 2); + + i++; + } + + console.log(numbers.join(',')); } 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..46d5cd4c 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,24 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + let i = 0; + + + while (i < birthdays.length) { + + let birthday = birthdays[i]; + + + if (birthday.includes('July')) { + + return birthday; + } + + i++; + } + + return null; + } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..187f7a07 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,15 @@ function evenNumbersSum(n) { // TODO + let sum = 0; + + for (let i = 0; i < n; i++) { + + sum += i * 2; + } + + return sum; + } 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..1f81ac9d 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,7 @@ // 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..a8263a95 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,13 @@ const AGES = [ ]; // TODO - Write for loop code here +for (let i = 0; i < WRITERS.length; i++) { + + let writer = WRITERS[i]; + let age = AGES[i]; + + console.log(`${writer} is ${age} 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..3b522e93 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,17 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (const station of tubeStations) { + + console.log(station); +} + // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for (const ch of str) { + console.log(ch.toUpperCase()); +} + + diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..24aceb0a 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,6 +13,19 @@ function getTemperatureReport(cities) { // TODO + + let reports = []; + + for (const city of cities) { + + let temperature = temperatureService(city); + + let report = `The temperature in ${city} is ${temperature} degrees`; + + reports.push(report); + } + + return reports; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..ae4ad009 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,13 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let num = generateRandomNumber(); + do { + num = generateRandomNumber(); + } while (num <= 50); + + return num; + } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..ba5b5a90 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -35,6 +35,19 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let averagePrices = []; + for (let stockPrices of closingPricesForAllStocks) { + let sum = 0; + + for (let price of stockPrices) { + sum += price; + } + + averagePrices.push(Math.round((sum / stockPrices.length) * 100) / 100); +} + +return averagePrices; + } /* @@ -49,6 +62,14 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + let priceChanges = []; + + for (let stockPrices of closingPricesForAllStocks) { + priceChanges.push(Math.round((stockPrices[stockPrices.length - 1] - stockPrices[0]) * 100) / 100); +} + +return priceChanges; + } /* @@ -65,6 +86,17 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + let highestPrices = []; +for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let stockPrices = closingPricesForAllStocks[i]; + let maxPrice = Math.max(...stockPrices); + + highestPrices.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${maxPrice}`); +} + +return highestPrices; + + }