diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..9bdf8e9b 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,12 +11,17 @@ // Example 1 let a; +// the variable a has not given a value() +// a = "Thank you for checking my work" console.log(a); // Example 2 function sayHello() { - let message = "Hello"; + let message = "Hello"; + /*the variable message is not called on the second line + example */ + // return message; } let hello = sayHello(); @@ -27,10 +32,13 @@ console.log(hello); function sayHelloToUser(user) { console.log(`Hello ${user}`); } - +// user doesn't have values in it sayHelloToUser(); +// sayHelloToUser("my friend"); // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// console.log(arr[2]) +//On the console have selected a number in the array which doesn't exit. javascript counts starting from 0 diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..63beccd5 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -7,6 +7,18 @@ function evenNumbers(n) { // TODO + i = 0; + y = n; + let x = ""; + if (n > 0) { + while (i < y) { + x += i + ","; + i += 2; + } + console.log(x); + } else { + // console.log("nothing"); + } } 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..e140d22e 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,14 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + // TODO + let capture = ""; + for (let i = 0; i < birthdays.length; i += 6) { + const index = birthdays[i]; + index; + capture += index; + console.log(index); + } } -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..f62477ed 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -8,6 +8,14 @@ function evenNumbersSum(n) { // TODO + let i = 0; + let message = "Total"; + let capture = ""; + do { + let sum = message + capture[i]; + console.log(sum); + i * 2; + } while (i < n); } 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..bd6f7191 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) { - console.log(String.fromCharCode(97 + i)); - i++; +// let i = 0; +// while(i < 26) { +// console.log(String.fromCharCode(97 + i)); +// i++; +// } +for (let i = 0; i < 29; i++) { + console.log(String.fromCharCode(94 + 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..2e425fa3 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -27,6 +27,18 @@ const AGES = [ ]; // TODO - Write for loop code here +let capture = ""; +for (let i = 0; i < WRITERS.length; i++) { + const name = WRITERS[i] + const age = AGES[i] + console.log(name, age, "years old") +} +// for (var i in WRITERS) { +// for (var a in AGES) { +// capture += WRITERS[i] + AGES[a]; +// console.log(capture); +// } +// } /* 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..a191f60d 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -12,5 +12,19 @@ let tubeStations = [ ]; -// TODO Use a for-of loop to capitalise and output each letter in the string seperately. +// TODO Use a for-of loop to capitalize and output each letter in the string seperately. let str = "codeyourfuture"; + +// TODO 1 +for (let i = 0; i < tubeStations.length; i++) { +const index = tubeStations[i]; +console.log(index); +} + +// TODO 2 +let capture = "" +for (let i = 0; i < str.length; i++) { + capture += str[i]; +} + +console.log(capture.toUpperCase()); \ No newline at end of file diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..04c0cc68 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,13 @@ */ function getTemperatureReport(cities) { - // TODO + // TODO + let temperature = []; + for (city of cities) { + let temp = temperatureService(city); + temperature.push("The temperature in", city, " is ", temp, " degrees"); + } + return temperature; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..1bd31b6e 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 + i = 0; + do { + i += generateRandomNumber(); + } while (i < 50); + return i; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..ad2b9a54 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,25 +6,36 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + const article = allArticleTitles.filter((article) => article.length <= 65); + return article; } /* The editor of the FT likes short headlines with only a few words! Implement the function below, which returns the title with the fewest words. - (you can assume words will always be seperated by a space) + (you can assume words will always be separated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + // 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! + The editor of the FT has realized 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. (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { // TODO -} + const title = allArticleTitles.filter((article) => article.match(/(\d+)/)); + return title /* The Financial Times wants to understand what the average number of characters in an article title is. @@ -32,6 +43,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..7e7420df 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,9 +34,14 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + // TODO + return closingPricesForAllStocks.map((priceArr) => { + return Number( + (priceArr.reduce((tot, num) => tot + num) / priceArr.length).toFixed(2) + ); + }); } - + /* We also want to see what the change in price is from the first day to the last day for each stock. Implement the below function, which @@ -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)); + }); } /*