diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..2e96371f 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,6 +12,7 @@ // Example 1 let a; console.log(a); +// does not have an assigned value // Example 2 @@ -21,7 +22,7 @@ function sayHello() { let hello = sayHello(); console.log(hello); - +// a value was not returned // Example 3 function sayHelloToUser(user) { @@ -29,8 +30,9 @@ function sayHelloToUser(user) { } sayHelloToUser(); - +// there is no any parameter when sayHelloToUser() is called // Example 4 -let arr = [1,2,3]; +let arr = [1, 2, 3]; console.log(arr[3]); +// ther is no value in the arr[3] \ No newline at end of file diff --git a/1-exercises/B-while-loop/exercise.js b/1-exercises/B-while-loop/exercise.js index b459888f..b1a59c65 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,14 @@ */ function evenNumbers(n) { - // TODO + let i = 0; + while (i < n * 2) { + console.log(i) + i += 2 + } } + evenNumbers(3); // should output 0,2,4 evenNumbers(0); // should output nothing evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18 diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..e3360012 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -17,7 +17,9 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + while (BIRTHDAYS.filter(item => item.match(/^July/g))) { + return BIRTHDAYS.find(item => item.includes('July')) + } } 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..57ef5245 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -7,9 +7,19 @@ */ function evenNumbersSum(n) { - // TODO + let i = 0, j = 0, k = 0 + if (n !== 0) { + do { + i += 2 + k += i + j++ + } while (j < n - 1) + return k + } else { + return 0 + } } console.log(evenNumbersSum(3)); // should output 6 console.log(evenNumbersSum(0)); // should output 0 -console.log(evenNumbersSum(10)); // should output 90 \ No newline at end of file +console.log(evenNumbersSum(10)); // should output 90 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..b42038c6 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..813a02b4 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -10,7 +10,12 @@ let tubeStations = [ "Oxford Street", "Tottenham Court Road" ]; - +for (let element of tubeStations) { + console.log(element) +} // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +for (let s of str) { + console.log(s.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..f9cc8f67 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,14 +12,18 @@ */ function getTemperatureReport(cities) { - // TODO + let arr = [] + for (let i = 0; i < cities.length; i++) { + arr.push(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`) + } + return arr } /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { - let temparatureMap = new Map(); + let temparatureMap = new Map(); temparatureMap.set('London', 10); temparatureMap.set('Paris', 12); @@ -28,7 +32,7 @@ function temperatureService(city) { temparatureMap.set('Mumbai', 29); temparatureMap.set('São Paulo', 23); temparatureMap.set('Lagos', 33); - + return temparatureMap.get(city); } diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..eacabcb3 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -5,20 +5,24 @@ // This function shouldn't be changed function generateRandomNumber() { - console.log("Generating number..."); - return Math.round(Math.random() * 100); + console.log('Generating number...'); + return Math.round(Math.random() * 100); } function getRandomNumberGreaterThan50() { - // TODO - implement using a do-while loop + let n; + do { + n = generateRandomNumber(); + } while (n < 50); + return n; } /* ======= TESTS - DO NOT MODIFY ===== */ -test("Returned value should always be greater than 50", () => { - expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); - expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); - expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); - expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); - expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); +test('Returned value should always be greater than 50', () => { + expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); + expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); + expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); + expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); + expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); }); diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..ffaac102 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -5,7 +5,7 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + return allArticleTitles.filter(item => item.length < 65) } /* @@ -14,7 +14,11 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let wordNums = [] + for (let i = 0; i < allArticleTitles.length; i++) { + wordNums.push(allArticleTitles[i].split(' ').length) + } + return allArticleTitles[wordNums.indexOf(Math.min(...wordNums))] } /* @@ -23,7 +27,12 @@ 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 NumberArray = [] + const regex = /[0-9]/g; + for (let i = 0; i < allArticleTitles.length; i++) { + if (allArticleTitles[i].search(regex) >= 0) { NumberArray.push(allArticleTitles[i]) } + } + return NumberArray } /* @@ -31,7 +40,11 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let sum = 0 + for (let i = 0; i < allArticleTitles.length; i++) { + sum += allArticleTitles[i].length + } + return Math.round(sum / allArticleTitles.length) } diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..47e38b80 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -34,7 +34,15 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + let arr = [] + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let sum = 0 + for (let j = 0; j < closingPricesForAllStocks[i].length; j++) { + sum += closingPricesForAllStocks[i][j] + } + arr.push(Number((sum / closingPricesForAllStocks[i].length).toFixed(2))) + } + return arr } /* @@ -48,7 +56,15 @@ 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 arr = [] + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let fiftyDay = closingPricesForAllStocks[i][closingPricesForAllStocks[i].length - 1] + let firstDay = closingPricesForAllStocks[i][0] + let diffNum = 0 + diffNum = fiftyDay - firstDay + arr.push(Number(diffNum.toFixed(2))) + } + return arr } /* @@ -64,7 +80,11 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let arr = [] + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + arr.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${(Math.max(...closingPricesForAllStocks[i])).toFixed(2)}`) + } + return arr } diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..50e70f8e 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 (; input > 0; input--) { + sum *= input + } + 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..b6d7e362 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,72 +11,84 @@ */ function getHighestRatedInEachGenre(books) { - // TODO + const result = books.reduce((acc, cur) => { + const groupByGenre = cur.genre; + if (!acc[groupByGenre]) { + acc[groupByGenre] = []; + } + acc[groupByGenre].push(cur); + return acc; + }, {}); + const genreName = Object.keys(result); + const arr = []; + for (let i = 0; i < genreName.length; i++) { + arr.push(result[genreName[i]].sort((a, b) => b.rating - a.rating)[0].title); + } + return arr; } - /* ======= 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 +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' + ]) + ); +}); diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..33291fb7 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,12 @@ */ function generateFibonacciSequence(n) { - // TODO + let arr = [] + const firstSecondNum = [0, 1] + for (let i = 0; i < n; i++) { + i === 0 || i === 1 ? arr.push(firstSecondNum[i]) : arr.push(arr[i - 2] + arr[i - 1]) + } + return arr } /* ======= TESTS - DO NOT MODIFY ===== */