diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..5e6af88a 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,6 +12,7 @@ // Example 1 let a; console.log(a); +// the reason why we are seeing undefined for a has no value assigned // Example 2 @@ -21,12 +22,15 @@ function sayHello() { let hello = sayHello(); console.log(hello); +// the function is undefined because it has not returned anything + // Example 3 function sayHelloToUser(user) { console.log(`Hello ${user}`); } +// the user parameter does not pass anything sayHelloToUser(); @@ -34,3 +38,5 @@ sayHelloToUser(); // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// there is no index 3 in the array the index goes up to 2 + diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..f7d7e86b 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 + const arr = [] + let i = 0 + + while (i < n) { + arr.push(i * 2); + i++; + } + console.log(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..9d08be2f 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,6 +17,15 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { + let i = 0; + + while (i < birthdays.length - 1) { + let birthdayCheck = birthdays[i]; + if (birthdayCheck.startsWith("July")) { + return birthdayCheck; + } + i++; + } // TODO } diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..a5371bcf 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..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..0561c380 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,9 @@ const AGES = [ ]; // TODO - Write for loop code here +for (let i = 0; i article.length <= 65); + return article; } /* @@ -15,8 +17,17 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + const outputOfArticle = allArticleTitles[0]; + for (let i = 0; i < allArticleTitles.length; i++) { + if ( + allArticleTitles[i].split(" ").length < outputOfArticle.split(" ").length + ) + outputOfArticle = allArticleTitles[i]; + } + return outputOfArticle; } + /* The editor of the FT has realised that headlines which have numbers in them get more clicks! Implement the function below to return a new array containing all the headlines which contain a number. @@ -24,6 +35,8 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + const title = allArticleTitles.filter((article) => article.match(/(\d+)/)); + return title } /* @@ -32,6 +45,12 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + const 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..73465f46 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -35,6 +35,11 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + return closingPricesForAllStocks.map((priceArr) => { + return Number( + (priceArr.reduce((tot, num) => tot + num) / priceArr.length).toFixed(2) + ); + }); } /* @@ -49,6 +54,9 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + return closingPricesForAllStocks.map((priceArr) => { + return Number((priceArr[priceArr.length - 1] - priceArr[0]).toFixed(2)); + }); } /* @@ -65,6 +73,13 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + let highestPriceDescriptions = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + highestPriceDescriptions.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[i] + ).toFixed(2)}` + ); + } + return highestPriceDescriptions; }