diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..2376449d 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- variable "a" has not been declared. let a; console.log(a); -// Example 2 +// Example 2- no value inside function parantheses so it doesnt returned any value. function sayHello() { let message = "Hello"; } @@ -23,7 +23,7 @@ let hello = sayHello(); console.log(hello); -// Example 3 +// Example 3- the function has been called with empty parameter function sayHelloToUser(user) { console.log(`Hello ${user}`); } @@ -31,6 +31,6 @@ function sayHelloToUser(user) { sayHelloToUser(); -// Example 4 +// Example 4- the index [3] does not exist in Array list. 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..673deb84 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,6 +7,17 @@ function evenNumbers(n) { // TODO + let i = 0; + let numbersList = []; + while (numbersList.length < n) { + if (i % 2 === 0) { + numbersList.push(i); + } + i++; + } + console.log(numbersList.toString()); + + } 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..3dc40b9c 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -18,6 +18,15 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + arrayLength = birthdays.length; + let i = 0; + while (i < arrayLength) { + 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..9793b4b3 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,13 @@ function evenNumbersSum(n) { // TODO + let sum = 0; + let i = 0; + do { + sum += i * 2; + i++; + } while (i < 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..aa95bb19 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,8 +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++; } diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..18c8cd44 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 < WRITERS.length; i++) { + console.log(WRITERS[i] + " is " + AGES[i] + "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..25ec436e 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -14,3 +14,6 @@ let tubeStations = [ // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (let i = 0; i < str.length; i ++) { + console.log(str[i].toUpperCase()) +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..8ef36089 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,6 +13,12 @@ function getTemperatureReport(cities) { // TODO + let temperatureReport = []; + for (city of cities) { + let cityTemp = temperatureService(city); + temperatureReport.push(`The temperature in ${city} is ${cityTemp} degrees`); + } + return temperatureReport; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..e70055c5 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 number; + do { + number = generateRandomNumber(); + } while (number <= 50); + return number; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..9c9c4d5a 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,6 +6,15 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + let headlines = []; + + for (let eachTitle of allArticleTitles) { + if (eachTitle.length <= 65) { + headlines.push(eachTitle); + } + } + + return headlines; } /* @@ -15,6 +24,19 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO + let leastWordsTillNow; + let headlineWithLeastWords; + + for (let eachHeadline of allArticleTitles) { + let numberOfWords = eachHeadline.split(" ").length; + + if (leastWordsTillNow === undefined || leastWordsTillNow > numberOfWords) { + leastWordsTillNow = numberOfWords; + headlineWithLeastWords = eachHeadline; + } + } + + return headlineWithLeastWords; } /* @@ -24,6 +46,15 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + let headlineTitlesWithNumbers = []; + + for (let eachHeadline of allArticleTitles) { + if (headlineTitleHasNumbers(eachHeadline)) { + headlineTitlesWithNumbers.push(eachHeadline); + } + } + + return headlineTitlesWithNumbers; } /* @@ -32,6 +63,13 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + let characterTotal = 0; + + for (let eachTitle of allArticleTitles) { + characterTotal += eachTitle.length; + } + + return Math.round(characterTotal / allArticleTitles.length); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..34dd6fe6 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -35,6 +35,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let average = []; + let stockSum = 0; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + stockSum = 0; + for (let j = 0; j < closingPricesForAllStocks[i].length; j++) { + stockSum = stockSum + closingPricesForAllStocks[i][j]; + } + average.push( + Number((stockSum / closingPricesForAllStocks[i].length).toFixed(2)) + ); + } + return average; } /* @@ -49,7 +61,16 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO -} + let priceChangeArr = []; + + for (price of closingPricesForAllStocks) { + let priceChange = Number((price[price.length - 1] - price[0]).toFixed(2)); + priceChangeArr.push(priceChange); + } + return priceChangeArr; + } + + /* As part of a financial report, we want to see what the highest price was for each stock in the last 5 days. @@ -65,6 +86,12 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + highestPriceLast5Days = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + highestPriceLast5Days.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[i]).toFixed(2)}`); + } + return highestPriceLast5Days; + }