diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..a1542889 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -9,12 +9,12 @@ For each example, can you explain why we are seeing undefined? */ -// Example 1 +// Example 1 - The reason this would return and undefined error is due to the variable 'a' not having a value assigned to it. let a; console.log(a); -// Example 2 +// Example 2 - This would be undefined as the function does not have a parameter. function sayHello() { let message = "Hello"; } @@ -23,7 +23,7 @@ let hello = sayHello(); console.log(hello); -// Example 3 +// Example 3 - This will be undefined as no user has been entered. function sayHelloToUser(user) { console.log(`Hello ${user}`); } @@ -31,6 +31,6 @@ function sayHelloToUser(user) { sayHelloToUser(); -// Example 4 +// Example 4 - This will be undefined as there is no 3rd index. let arr = [1,2,3]; console.log(arr[3]); diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..651a70c9 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,6 +7,14 @@ function evenNumbers(n) { // TODO + let i = 0 + const arr = [] + while (i < n && n > 0){ + arr.push(i * 2) + console.log(arr[i]) + i++ + } + return arr.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..a611b52c 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,8 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + const findFirstJulyBDay = birthdays.filter((bDay) => bDay.startWith("July")) + return findFirstJulyBDay[0] } 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..d6244f88 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,17 @@ function evenNumbersSum(n) { // TODO + if (n == 0) return 0 + let i = 0 + j = 1 + sum = 0 +do { + i += 2 + sum += i + j++ +} +while (j < n) +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..cfbf2196 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -7,8 +7,8 @@ // 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..0bd255f7 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 article.length <= 65) } /* @@ -15,6 +16,12 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + let result = allArticleTitles[0] + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].split(' ').length < result.split(' ').length) + result = allArticleTitles[i]; + } + return result } /* @@ -24,14 +31,23 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + return allArticleTitles.filter((article) => article.match(/(\d+)/)); } /* 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. */ + + function averageNumberOfCharacters(allArticleTitles) { // TODO + let total = 0; + for (let title of allArticleTitles) { + total += title.length; + } + let average = total / allArticleTitles.length; + return Math.round(average); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..7172f4f7 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 stockPriceAverage = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let stockPriceTotal = 0; + let stockPrice = closingPricesForAllStocks[i]; + for (let j = 0; j < stockPrice.length; j++) { + stockPriceTotal += stockPrice[j]; + } + stockPriceAverage.push( + parseFloat((stockPriceTotal / stockPrice.length).toFixed(2)) + ); + } + return stockPriceAverage; + } /* @@ -49,6 +62,14 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + changedPrice = []; + for (const price of closingPricesForAllStocks) { + let priceDifference = parseFloat( + (price[price.length - 1] - price[0]).toFixed(2) + ); + changedPrice.push(priceDifference); + } + return changedPrice; } /* @@ -65,6 +86,17 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + let highestPriceForEachStock = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + highestPriceForEachStock.push( + `The highest price of ${stocks[ + i + ].toUpperCase()} in the last 5 days was ${Math.max( + ...closingPricesForAllStocks[i] + ).toFixed(2)}` +); + } + return highestPriceForEachStock; }