From e53c1b6bdea8e469b78a45c3f64cba72a04cb8d1 Mon Sep 17 00:00:00 2001 From: tony-dev-wm <111275895+Tony-devops@users.noreply.github.com> Date: Sat, 3 Dec 2022 09:19:45 +0000 Subject: [PATCH 1/3] exercises --- 1-exercises/A-undefined/exercise.js | 7 ++++--- 1-exercises/B-while-loop/exercise.js | 7 +++++++ 1-exercises/C-while-loop-with-array/exercise.js | 7 +++++++ 1-exercises/D-do-while/exercise.js | 11 +++++++++++ 1-exercises/E-for-loop/exercise1.js | 5 +++-- 1-exercises/E-for-loop/exercise2.js | 3 +++ 1-exercises/F-for-of-loop/exercise.js | 8 +++++++- 7 files changed, 42 insertions(+), 6 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..9219896b 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,9 +12,10 @@ // Example 1 let a; console.log(a); +// value not assigned -// Example 2 +// Example 2 message not assigned function sayHello() { let message = "Hello"; } @@ -23,7 +24,7 @@ let hello = sayHello(); console.log(hello); -// Example 3 +// Example 3 no parameter assigned function sayHelloToUser(user) { console.log(`Hello ${user}`); } @@ -31,6 +32,6 @@ function sayHelloToUser(user) { sayHelloToUser(); -// Example 4 +// Example 4 array is only 3 objects and starts at 0. no ob at pos 3 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..5c04a357 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,6 +7,13 @@ function evenNumbers(n) { // TODO + const arr = []; + let i = 0; + while(i < n * 2) { + arr.push(i); + i +=2; + } + return arr; } 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..dfe20571 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,13 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + let i = 0; + while(i < birthdays.length){ + if( birthdays[i].includes("July")){ + return birthdays[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..49af98c3 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 + let i = 0, j = 0, k =0 + if (n != 0) { + do { + i += 2 + k += i + j++ + } while(j < n-1) + return k + } else { + return 0 + } } 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..402be95f 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,10 @@ // 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. + \ No newline at end of file diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..03da286f 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 Date: Thu, 8 Dec 2022 12:34:06 +0000 Subject: [PATCH 2/3] exercises --- 2-mandatory/1-weather-report.js | 7 +++++ 2-mandatory/2-retrying-random-numbers.js | 5 ++++ 2-mandatory/3-financial-times.js | 22 +++++++++++++++ 2-mandatory/4-stocks.js | 13 +++++++++ 3-extra/1-factorial.js | 7 +++++ 3-extra/2-array-of-objects.js | 36 ++++++++++++++++++++++++ 3-extra/3-fibonacci.js | 12 ++++++++ 7 files changed, 102 insertions(+) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..44eb2891 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,6 +13,13 @@ function getTemperatureReport(cities) { // TODO + let cityTemp = []; + for (let city of cities){ + cityTemp.push( + `The temeprature in ${city} is ${temperatureService(city)} degrees ` + ); + } + return cityTemp; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..1a863dd4 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,11 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let num = 0; + do { + num = generateRandomNumber(); + } while (num < 50); + return num; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..bd50b318 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,6 +6,11 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + let headline = [] + for (article of allArticleTitles){ + if (article.length < 65) headline.push(article) ; + } + return headline; } /* @@ -15,6 +20,11 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + let lowestWords = []; + for (let i = 0; i< allArticleTitles.length; i++) { + lowestWords.push(allArticleTitles[i].split(' ').length) + } + return allArticleTitles[lowestWords.indexOf(Math.min(...lowestWords))] } /* @@ -24,6 +34,13 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + let numberArray = []; + for (let article of allArticleTitles) { + if (/[0-9]/.test(article) === true) { + numberArray.push(article); + } + } + return numberArray; } /* @@ -32,6 +49,11 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + let averageCharacter = []; + for (let i = 0; i < allArticleTitles.length; i++) { + averageCharacter += allArticleTitles[i].length + } + return Math.round(averageCharacter / allArticleTitles.length) } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..92522c32 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 averagePriceArray = []; + for (let individualArray of closingPricesForAllStocks) { + averagePriceArray.push(individualAverage(individualArray)); + } + return averagePriceArray; + +} +function individualAverage(array) { + let total = 0; + for (let closing of array) { + total += closing; + } + return Number(total / array.length).toFixed(2); } /* diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..b1ed8241 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -10,6 +10,13 @@ function factorial(input) { // TODO + let total = 1; + for (i = 1; i<=input; i++) { + if (input > 1) { + total *= i; + } + } + return total; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..d8f4bb5b 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -9,9 +9,45 @@ Implement a function which takes the array of books as a parameter, and returns an array of book titles. Each title in the resulting array should be the highest rated book in its genre. */ +// first get a list of all genres that are unique +// next would be to get the highest rated in that genre +//final would be to loop thru the book array and get the title with highest rating + +function uniqueGenres(books) { + let genres = []; + for (let book of books) { + genres.push (book.genre) + } + return [...new Set(genres)]; +} + +// highest rated title in each genres +function highestRated(books, genre) { + let ratingArray = []; + for( let book of books) { + if (book.genre === genre) { + ratingArray.push(book.rating); + } + } + return Math.max(...ratingArray); +} + + function getHighestRatedInEachGenre(books) { // TODO + let list = []; + let genres = uniqueGenres(books); + for (let book of books) { + for (let genre of genres) { + if ( + book.rating === highestRated(books, genre) && book.genre === genre + ) { + list.push(book.title); + } + } + } + return list; } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..dbdfdee9 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -15,6 +15,18 @@ function generateFibonacciSequence(n) { // TODO + let newSequence = []; + let amount =0; + for (let i = 0; i=2) { + amount = newSequence[i -1] + newSequence[i -2]; + newSequence.push(amount); + } else { + amount += i; + newSequence.push (amount); + } + } + return newSequence; } /* ======= TESTS - DO NOT MODIFY ===== */ From f253af4181c095d36df7dbb819e179c0c773506e Mon Sep 17 00:00:00 2001 From: tony-dev-wm Date: Thu, 8 Dec 2022 18:54:25 +0000 Subject: [PATCH 3/3] exercises --- 2-mandatory/4-stocks.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 92522c32..7711ba0a 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -62,6 +62,12 @@ function individualAverage(array) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + let priceChanges = []; + for (let priceArray of closingPricesForAllStocks) { + let priceChanges = [priceArray.length-1] - priceArray[0]; + priceChanges.push(Number(priceChanges.toFixed(2))); + } + return priceChanges; } /* @@ -78,6 +84,14 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + let maxedStock = []; + for (i = 0; i < stocks.length; i++) { + let capitaliseStock = stocks[i].toUpperCase(); + let highestPrice = Math.max(...closingPricesForAllStocks[i]); + let decimalPrice = highestPrice.toFixed(2); + maxedStock.push(`The highest price of ${capitaliseStock} in the last five days was ${decimalPrice}`); + } + return maxedStock; }