diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..7a9dfa04 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "pwa-chrome", + "request": "launch", + "name": "Launch Chrome against localhost", + "url": "http://localhost:8080", + "webRoot": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..8781f4d0 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 has not been assigned let a; console.log(a); -// Example 2 +// Example 2 - value in the function was not returned function sayHello() { let message = "Hello"; } @@ -23,7 +23,7 @@ let hello = sayHello(); console.log(hello); -// Example 3 +// Example 3 - function has no parameter function sayHelloToUser(user) { console.log(`Hello ${user}`); } @@ -31,6 +31,6 @@ function sayHelloToUser(user) { sayHelloToUser(); -// Example 4 +// Example 4 - array does not have index number 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..d66b9e19 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,7 +6,17 @@ */ function evenNumbers(n) { - // TODO + let i = 0; + let j = 0; + let res = ""; + while (i < n) { + if (j % 2 === 0) { + res += j + ","; + i++; + } + j++; + } + console.log(res.slice(0, -1)); } 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..fec1b8e2 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -16,8 +16,14 @@ const BIRTHDAYS = [ "November 15th" ]; + function findFirstJulyBDay(birthdays) { - // TODO + let i = 0; + const month = "July"; + while (!(birthdays[i].includes(month))) { + i++; + } +return birthdays[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..2fbc8cc9 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,7 +7,17 @@ */ function evenNumbersSum(n) { - // TODO + let i = 0; + let j = 0; + let sum = 0; + do { + if (j % 2 === 0) { + sum += j; + i++; + } + j++; + } 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..63f77dad 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,8 @@ // 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. diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..38f5ac47 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 (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..6d4dd50e 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,13 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (const station of tubeStations) { + console.log(station); +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for (const char of str) { + console.log(char.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..bdd73aef 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,11 @@ */ function getTemperatureReport(cities) { - // TODO + let res = []; + for (const city of cities) { + res.push(`The temperature in ${city} is ${temperatureService(city)} degrees`); + } + return res; } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..33e0d96c 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -10,7 +10,11 @@ function generateRandomNumber() { } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let i = 0; + do { + i = generateRandomNumber(); + } while (i < 51); + return i; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..8cec556f 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,7 +5,8 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + let shortArticles = allArticleTitles.filter(title => title.length < 66); + return shortArticles; } /* @@ -13,8 +14,15 @@ function potentialHeadlines(allArticleTitles) { Implement the function below, which returns the title with the fewest words. (you can assume words will always be seperated by a space) */ + function titleWithFewestWords(allArticleTitles) { - // TODO + let lengthOfTitles = []; + for (const articleTitle of allArticleTitles) { + lengthOfTitles.push(articleTitle.split(" ").length - 1); + } + const min = Math.min(...lengthOfTitles); + const index = lengthOfTitles.indexOf(min); + return allArticleTitles[index]; } /* @@ -23,15 +31,23 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + let digits = /\d+/; + let articlesWithNumbers = allArticleTitles.filter(articles => articles.match(digits)); + return articlesWithNumbers; } + /* 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. */ +let sumOfTitlesLength = 0; function averageNumberOfCharacters(allArticleTitles) { - // TODO + for (const articleTitle of allArticleTitles) { + sumOfTitlesLength += articleTitle.length; + } + let averageChar = sumOfTitlesLength / allArticleTitles.length; + return Math.round(averageChar); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..9f2f5292 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,19 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let allAveragePrices = []; + + for (const closingPricesForStock of closingPricesForAllStocks) { + let sum = 0; + let average = 0; + + for (const price of closingPricesForStock) { + sum += price; + } + average = sum / closingPricesForStock.length; + allAveragePrices.push(parseFloat(average.toFixed(2))); + } + return allAveragePrices; } /* @@ -48,7 +60,18 @@ 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 + let changeInPrice = []; + let firstPrice = 0; + let lastPrice = 0; + let difference = 0; + + for (const closingPricesForStock of closingPricesForAllStocks) { + firstPrice = closingPricesForStock[0]; + lastPrice = closingPricesForStock[closingPricesForAllStocks.length - 1]; + difference = lastPrice - firstPrice; + changeInPrice.push(parseFloat(difference.toFixed(2))); + } + return changeInPrice; } /* @@ -64,7 +87,15 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let highestPriceForAllStock = []; + let highestPrice = 0; + + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + highestPrice = Math.max(...closingPricesForAllStocks[i]); + highestPrice = highestPrice.toFixed(2); + highestPriceForAllStock[i] = `The highest price of ${stocks[i].toUpperCase()} in the last ${closingPricesForAllStocks.length} days was ${highestPrice}`; + } + return highestPriceForAllStock; } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..0a57dbde 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,7 +9,11 @@ */ function factorial(input) { - // TODO + let sum = 1; + for (let i = input; i >= 1; i--) { + sum *= i; + } + return sum; } /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..ac3088b0 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,7 +11,17 @@ */ function getHighestRatedInEachGenre(books) { - // TODO + let sortedBooks = books.sort((b1, b2) => b1.rating < b2.rating ? 1 : b1.rating > b2.rating ? -1 : 0); + let highestRatedBook = []; + let arrOfGenres = []; + + for (const book of sortedBooks) { + if (!arrOfGenres.includes(book.genre)) { + arrOfGenres.push(book.genre); + highestRatedBook.push(book.title); + } + } + return highestRatedBook; } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..9f6597be 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,14 @@ */ function generateFibonacciSequence(n) { - // TODO + sequence = [0, 1]; + let newNum = 0; + + for (let i = 2; i < n; i++) { + newNum = sequence[i - 2] + sequence[i - 1]; + sequence.push(newNum); + } + return sequence; } /* ======= TESTS - DO NOT MODIFY ===== */