diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..e65dd4cf 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,6 +12,8 @@ // Example 1 let a; console.log(a); +//a was not assigned any value + // Example 2 @@ -21,6 +23,7 @@ function sayHello() { let hello = sayHello(); console.log(hello); +//The variable "sayHello" were not passed any value // Example 3 @@ -29,8 +32,12 @@ function sayHelloToUser(user) { } sayHelloToUser(); +//Again the function "sayHelloToUser", has not been passed any arguments // Example 4 let arr = [1,2,3]; console.log(arr[3]); +// In this example above, there are no index value 3 in the array. +// Arrays work with zero indexing and therefore a index of 3 would indicate a fourth value in the array +// and this array only has three values and result to an undefined error. diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..40aebfb6 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 + let i = 0; + let evenArray = []; + + while (i < n) { + evenArray.push(i * 2); + i++; + } + console.log(evenArray.join(', ')); } 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..88350f98 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,19 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + //TODO + let i = 0; + let myBdayCheck; + + while (i < birthdays.length - 1) { + myBdayCheck = birthdays[i]; + + if (myBdayCheck.startsWith("July")) { + return myBdayCheck; + } + 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..ccef58f7 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 sum = 0; + + do { + sum = 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..dcddc087 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -6,9 +6,9 @@ // 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..5e7151f9 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -28,6 +28,11 @@ 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..7b3f63ba 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -11,6 +11,14 @@ let tubeStations = [ "Tottenham Court Road" ]; +for (let stations of tubeStations) { + console.log(stations); +} + // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; + +for (let letter of str) { + console.log(letter.toUpperCase()); +} diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..f8ce6fa1 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -7,15 +7,23 @@ Implement the function below: - take the array of cities as a parameter - return an array of strings, which is a statement about the temperature of each city. - For example, "The temperature in London is 10 degrees" + For example, "" - Hint: you can call the temperatureService function from your function */ function getTemperatureReport(cities) { // TODO + +let tempArray = []; +for (let city of cities) { + tempArray.push(`The temperature in ${city} is ${temperatureService(city)} degrees`); +} +return tempArray; + } + /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..e9f1560c 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -11,6 +11,12 @@ function generateRandomNumber() { function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop + let num; + 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..51c096a3 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -6,6 +6,16 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + let fitTitles = []; + for (let articleTitle of allArticleTitles) { + + if (articleTitle.length <= 65) { + fitTitles.push(articleTitle); + } + + } + + return fitTitles; } /* @@ -15,7 +25,26 @@ function potentialHeadlines(allArticleTitles) { */ function titleWithFewestWords(allArticleTitles) { // TODO -} + let fewest = allArticleTitles[0]; + let prevCount = allArticleTitles[0].split(" ").length; + + for(let i = 1; i < allArticleTitles.length; i++) { + + let currentCount = allArticleTitles[i].split(" ").length; + + if(currentCount < prevCount) { + fewest = allArticleTitles[i]; + } + + prevCount = currentCount; + } + + return fewest; + + } + + + /* The editor of the FT has realised that headlines which have numbers in them get more clicks! @@ -24,7 +53,26 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO -} + let numberHeadlines = []; + + for(let i = 0; i < allArticleTitles.length; i++) { + let titleChecker = allArticleTitles[i]; + + for( let j = 0; j < titleChecker.length; j++) { + + let charChecker = Number(titleChecker[j]) + + if(Number.isInteger(charChecker) && charChecker > 0 ){ + numberHeadlines.push(titleChecker); + break; + } + } + } + return numberHeadlines; + } + + + /* The Financial Times wants to understand what the average number of characters in an article title is. @@ -32,6 +80,17 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + + // let averageNum = 0; + let titleSum = 0; + + for (let i = 0; i < allArticleTitles.length; i++){ + titleSum += allArticleTitles[i].length; + } + + let averageNum = titleSum / allArticleTitles.length; + + return Math.round(averageNum); } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..e0e26521 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -35,6 +35,17 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + + let averageStockArr = []; + for(let i = 0; i < closingPricesForAllStocks.length; i++){ + let sumOfStock = 0; + for( let j = 0; j < closingPricesForAllStocks[i].length; j++ ) { + sumOfStock += closingPricesForAllStocks[i][j]; + } + let averageSum = sumOfStock / closingPricesForAllStocks.length; + averageStockArr.push(parseFloat(averageSum.toFixed(2))); + } + return averageStockArr; } /* @@ -49,6 +60,17 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + + let priceChangeArr = []; + + for(let i = 0; i < closingPricesForAllStocks.length; i++){ + let last = closingPricesForAllStocks[i].length - 1; + let firstPrice = closingPricesForAllStocks[i][0]; + let lastPrice = closingPricesForAllStocks[i][last]; + let averagePriceSum = lastPrice - firstPrice; + priceChangeArr.push(parseFloat(averagePriceSum.toFixed(2))); + } + return priceChangeArr; } /* @@ -65,9 +87,24 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + let highestStockPrice = []; + + for(let i = 0; i < closingPricesForAllStocks.length; i++){ + let highTotal = 0; + for(let j = 0; j < closingPricesForAllStocks[i].length; j++){ + if(closingPricesForAllStocks[i][j] > highTotal){ + highTotal = closingPricesForAllStocks[i][j]; + } + } + highestStockPrice.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highTotal.toFixed(2)}`); + } + + + return highestStockPrice; } + /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the average price for each stock", () => { expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..98b70a1e 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,19 +9,26 @@ */ function factorial(input) { - // TODO + // TODO + + let sum = input; + + for (let i = input - 1; i >= 1; i--) { + sum = sum * i; + } + return sum; } /* ======= TESTS - DO NOT MODIFY ===== */ test("3! should be 6", () => { - expect(factorial(3)).toEqual(6); + expect(factorial(3)).toEqual(6); }); test("5! should be 120", () => { - expect(factorial(5)).toEqual(120); + expect(factorial(5)).toEqual(120); }); test("10! should be 3628800", () => { - expect(factorial(10)).toEqual(3628800); + expect(factorial(10)).toEqual(3628800); }); diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..a6d0679e 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -10,73 +10,106 @@ Each title in the resulting array should be the highest rated book in its genre. */ +// TODO + function getHighestRatedInEachGenre(books) { - // TODO -} + let genresArr = []; + let highestRating = ""; + let highestTitle = ""; + + function createGenreArray(books) { + books.forEach((book) => { + if (!genresArr.includes(book.genre)) genresArr.push(book.genre); + }); + return genresArr; + } + + function getGenreBooks(genre) { + let genreBooksArray = books.filter((book) => book.genre === genre); + return genreBooksArray; + } + function highestGenre(genreBook) { + highestRating = ""; + highestTitle = ""; + genreBook.forEach((book) => { + if (book.rating > highestRating) { + highestRating = book.rating; + highestTitle = book.title; + } + }); + return highestTitle; + } + + let highestTitlesArr = createGenreArray(books).map((genre) => { + let booksByGenre = getGenreBooks(genre); + return highestGenre(booksByGenre); + }); + return highestTitlesArr; //highestTitles array; +} /* ======= Book data - DO NOT MODIFY ===== */ const BOOKS = [ - { - title: "The Lion, the Witch and the Wardrobe", - genre: "children", - rating: 4.7 - }, - { - title: "Sapiens: A Brief History of Humankind", - genre: "non-fiction", - rating: 4.7 - }, - { - title: "Nadiya's Fast Flavours", - genre: "cooking", - rating: 4.7 - }, - { - title: "Harry Potter and the Philosopher's Stone", - genre: "children", - rating: 4.8 - }, - { - title: "A Life on Our Planet", - genre: "non-fiction", - rating: 4.8 - }, - { - title: "Dishoom: The first ever cookbook from the much-loved Indian restaurant", - genre: "cooking", - rating: 4.85 - }, - { - title: "Gangsta Granny Strikes Again!", - genre: "children", - rating: 4.9 - }, - { - title: "Diary of a Wimpy Kid", - genre: "children", - rating: 4.6 - }, - { - title: "BOSH!: Simple recipes. Unbelievable results. All plants.", - genre: "cooking", - rating: 4.6 - }, - { - title: "The Book Your Dog Wishes You Would Read", - genre: "non-fiction", - rating: 4.85 - }, -] - + { + title: "The Lion, the Witch and the Wardrobe", + genre: "children", + rating: 4.7, + }, + { + title: "Sapiens: A Brief History of Humankind", + genre: "non-fiction", + rating: 4.7, + }, + { + title: "Nadiya's Fast Flavours", + genre: "cooking", + rating: 4.7, + }, + { + title: "Harry Potter and the Philosopher's Stone", + genre: "children", + rating: 4.8, + }, + { + title: "A Life on Our Planet", + genre: "non-fiction", + rating: 4.8, + }, + { + title: + "Dishoom: The first ever cookbook from the much-loved Indian restaurant", + genre: "cooking", + rating: 4.85, + }, + { + title: "Gangsta Granny Strikes Again!", + genre: "children", + rating: 4.9, + }, + { + title: "Diary of a Wimpy Kid", + genre: "children", + rating: 4.6, + }, + { + title: "BOSH!: Simple recipes. Unbelievable results. All plants.", + genre: "cooking", + rating: 4.6, + }, + { + title: "The Book Your Dog Wishes You Would Read", + genre: "non-fiction", + rating: 4.85, + }, +]; /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the highest rated book in each genre", () => { - expect(new Set(getHighestRatedInEachGenre(BOOKS))).toEqual(new Set( - [ - "The Book Your Dog Wishes You Would Read", - "Gangsta Granny Strikes Again!", - "Dishoom: The first ever cookbook from the much-loved Indian restaurant" - ] - )); -}); \ No newline at end of file + expect(new Set(getHighestRatedInEachGenre(BOOKS))).toEqual( + new Set([ + "The Book Your Dog Wishes You Would Read", + "Gangsta Granny Strikes Again!", + "Dishoom: The first ever cookbook from the much-loved Indian restaurant", + ]) + ); +}); diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..7fcffaf1 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,24 +14,36 @@ */ function generateFibonacciSequence(n) { - // TODO + // TODO + + let fibArr = []; + + for (i = 0; i < n; i++) { + if (i === 0) { + fibArr.push(i); + } else if (i === 1) { + fibArr.push(i); + } else { + fibArr.push(fibArr[i - 1] + fibArr[i - 2]); + console.log(fibArr[i - 1] + fibArr[i - 2]); + } + } + return fibArr; } /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the first 10 numbers in the Fibonacci Sequence", () => { - expect(generateFibonacciSequence(10)).toEqual( - [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] - ); + expect(generateFibonacciSequence(10)).toEqual([ + 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, + ]); }); test("should return the first 5 numbers in the Fibonacci Sequence", () => { - expect(generateFibonacciSequence(5)).toEqual( - [0, 1, 1, 2, 3] - ); + expect(generateFibonacciSequence(5)).toEqual([0, 1, 1, 2, 3]); }); test("should return the first 15 numbers in the Fibonacci Sequence", () => { - expect(generateFibonacciSequence(15)).toEqual( - [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377] - ); -}); \ No newline at end of file + expect(generateFibonacciSequence(15)).toEqual([ + 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, + ]); +});