diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..d7bc9542 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,12 +11,12 @@ // Example 1 let a; -console.log(a); +console.log(a); //a is not defined // Example 2 function sayHello() { - let message = "Hello"; + let message = "Hello"; //the function doesn`t assigned to a value } let hello = sayHello(); @@ -26,11 +26,11 @@ console.log(hello); // Example 3 function sayHelloToUser(user) { console.log(`Hello ${user}`); -} +} //the functin doesn`t have return value sayHelloToUser(); // Example 4 let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]); //there is no [3]index just 0,1,2 diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..9ae75be4 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 + if (n > 0) + let x = ""; + let i = 0; + while (i < n) { + x = x + "," + i * 2: + i++; + } + console.log(x.slice(1, x.length)); } 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..59f95f4c 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,15 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + // TODO + let i = 0; + let birthday = ""; + while (i < BIRTHDAYS.length) { + if (BIRTHDAYS[i].includes("July")) { + birthday = BIRTHDAYS[i]; + return birthday; + } + i++; } - -console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" +}; +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..1c552495 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,15 @@ function evenNumbersSum(n) { // TODO + let total = 0; + let i = 0; + do { + if (i % 2 === 0) { + total += i; + } + i++; + } while (i < n * 2); + return total; } 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..94211c08 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,12 @@ // Change the below code to use a for loop instead of a while loop. -let i = 0; -while(i < 26) { +// 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..63aa9463 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 < 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..a30e7add 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -8,9 +8,19 @@ let tubeStations = [ "Baker Street", "Picadilly Circus", "Oxford Street", - "Tottenham Court Road" + "Tottenham Court Road", ]; +function outputStations(array) { + for (let i = 0; i < array.length; i++) { + console.log(`${array[i]}`); + } + } // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +function outputStations(array) { + for (let i = 0; i < array.length; i++) { + console.log(`${array[i]}`); + } + } \ No newline at end of file diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..5291b9e3 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,6 +13,15 @@ function getTemperatureReport(cities) { // TODO + let stringOutput = []; + for (let i = 0; i < cities.length; i++) { + stringOutput.push( + `The temperature in ${cities[i]} is ${temperatureService( + cities[i] + )} degrees` + ); + } + return stringOutput; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..dfd28047 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 x = 0 + do { + x = generateRandomNumber() + } while (x <= 50) + return x } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..d676be7a 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,6 +6,7 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + return allArticleTitles.filter((article) => 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,21 @@ 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. */ +const reducer = (previousValue, currentValue) => previousValue + currentValue + function averageNumberOfCharacters(allArticleTitles) { // TODO + return Math.round( + allArticleTitles.map((article) => article.length).reduce(reducer) / + allArticleTitles.length, + ) } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..723af79d 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -33,8 +33,15 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Solve the smaller problems, and then build those solutions back up to solve the larger problem. Functions can help with this! */ +const reducer = (previousValue, currentValue) => previousValue + currentValue + function getAveragePrices(closingPricesForAllStocks) { // TODO + return closingPricesForAllStocks.map((priceList) => + parseFloat( + (priceList.slice(priceList.length - 5).reduce(reducer) / 5).toFixed(2), + ), + ) } /* @@ -49,6 +56,10 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + const result = closingPricesForAllStocks.map((priceList) => + priceList.slice(priceList.length - 5), + ) + return result.map((res) => parseFloat((res[4] - res[0]).toFixed(2))) } /* @@ -65,6 +76,16 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + const result = closingPricesForAllStocks.map((priceList) => + priceList.slice(priceList.length - 5), + ) + return result.map( + (res, index) => + 'The highest price of ' + + stocks[index].toUpperCase() + + ' in the last 5 days was ' + + Math.max(...res).toFixed(2), + ) }