diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..a792a99d 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,7 +6,15 @@ */ 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..ad5b8153 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,12 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO +let i = 0 +while (i<= BIRTHDAYS.length) { + let a = birthdays[5] + console.log(a[i]) + i++ +} } 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..c237a727 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,11 @@ */ function evenNumbersSum(n) { - // TODO + i=0 + do { + console.log(i) + i++ + } while (i<=5); } 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..fa68fddf 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) { - console.log(String.fromCharCode(97 + i)); - i++; + +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..3f393e23 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -26,7 +26,9 @@ const AGES = [ 49 ]; -// TODO - Write for loop code here +for (let i = 0; i article.length <= 65) } /* @@ -14,7 +14,12 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ 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 } /* @@ -23,7 +28,7 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + return allArticleTitles.filter((article) => article.match(/(\d+)/)); } /* @@ -31,7 +36,12 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let i = 0; + for (let title of allArticleTitles) { + i += 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..3086e5f9 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,9 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + return closingPricesForAllStocks.map(priceArr => { + return Number((priceArr.reduce((tot, num) => tot + num) / priceArr.length).toFixed(2)); + }) } /* @@ -48,7 +50,9 @@ 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 + return closingPricesForAllStocks.map(priceArr => { + return Number((priceArr[priceArr.length - 1] - priceArr[0]).toFixed(2)); + }) } /*