diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..6f1816cd 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,8 +11,7 @@ // Example 1 let a; -console.log(a); - +console.log(a); // return undefined because a variable that has not been assigned a value // Example 2 function sayHello() { @@ -20,17 +19,15 @@ function sayHello() { } let hello = sayHello(); -console.log(hello); - +console.log(hello); // return undefined because value (message variable) was not returned. // Example 3 function sayHelloToUser(user) { - console.log(`Hello ${user}`); + console.log(`Hello ${user}`); // return undefined because when call the function it is not have a argument } sayHelloToUser(); - // Example 4 -let arr = [1,2,3]; -console.log(arr[3]); +let arr = [1, 2, 3]; +console.log(arr[3]); // return undefined because the value has not been assigned in index 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..1fbf3a3f 100644 --- a/1-exercises/B-while-loop/exercise.js +++ b/1-exercises/B-while-loop/exercise.js @@ -6,9 +6,19 @@ */ function evenNumbers(n) { - // TODO + let count = 0; + let result = []; + if (n) { + while (count < n) { + // count === 0 ? result.push(count) : result.push(count * 2); + result.push(count * 2); + count++; + } + } + console.log(result); + console.log("--------------"); } 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 +evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18 \ No newline at end of file diff --git a/1-exercises/C-while-loop-with-array/exercise.js b/1-exercises/C-while-loop-with-array/exercise.js index d584cd75..48b43060 100644 --- a/1-exercises/C-while-loop-with-array/exercise.js +++ b/1-exercises/C-while-loop-with-array/exercise.js @@ -13,11 +13,28 @@ const BIRTHDAYS = [ "July 11th", "July 17th", "September 28th", - "November 15th" + "November 15th", ]; +// let July1 = BIRTHDAYS.filter((x) => x.split(" ")[0] === "July"); +// console.log(July1.sort()[0]); + +// Good use of split and index here.You could also use "break" +// to exit the +// while loop once you have found the first July date. + function findFirstJulyBDay(birthdays) { - // TODO + let july = []; + let count = 0; + + while (count < birthdays.length) { + if (birthdays[count].split(" ")[0] === "July") { + july.push(birthdays[count]); + break; + } + count++; + } + return july.sort()[0]; } -console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" +console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" \ No newline at end of file diff --git a/1-exercises/D-do-while/exercise.js b/1-exercises/D-do-while/exercise.js index f10d0764..2133bb2c 100644 --- a/1-exercises/D-do-while/exercise.js +++ b/1-exercises/D-do-while/exercise.js @@ -5,9 +5,28 @@ Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0) */ +//💫 +// You need to re-read the question. You need the loop to add up the first n even numbers, +// starting at 0. So if n=3, you need to add up 0+2+4. If n=10 you +// need to add up 0+2+4+6+8+10+12+14+16+18. +//💫 function evenNumbersSum(n) { - // TODO + let result = [0]; + // for (let i = 1; i < n; i++) { + // result[i] = i * 2 + result[i - 1]; + // } + // let total = result[result.length - 1]; + // return total; + + let total; + let i = 1; + do { + result[i] = i * 2 + result[i - 1]; + total = result[result.length - 1]; + i++; + } while (i < n); + 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..168dbccf 100644 --- a/1-exercises/E-for-loop/exercise1.js +++ b/1-exercises/E-for-loop/exercise1.js @@ -4,11 +4,14 @@ Change the while loop below into a for loop. */ - // 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++; +// } // The output shouldn't change. + +for (let i = 0; i < 26; i++) { + console.log(String.fromCharCode(65 + i), String.fromCharCode(97 + i)); +} \ No newline at end of file diff --git a/1-exercises/E-for-loop/exercise2.js b/1-exercises/E-for-loop/exercise2.js index 081002b2..aff45b71 100644 --- a/1-exercises/E-for-loop/exercise2.js +++ b/1-exercises/E-for-loop/exercise2.js @@ -15,19 +15,15 @@ const WRITERS = [ "Zadie Smith", "Jane Austen", "Bell Hooks", - "Yukiko Motoya" -] - -const AGES = [ - 59, - 40, - 41, - 63, - 49 + "Yukiko Motoya", ]; -// TODO - Write for loop code here +const AGES = [59, 40, 41, 63, 49]; +// 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: @@ -36,4 +32,4 @@ Zadie Smith is 40 years old Jane Austen is 41 years old Bell Hooks is 63 years old Yukiko Motoya is 49 years old -*/ +*/ \ No newline at end of file diff --git a/1-exercises/F-for-of-loop/exercise.js b/1-exercises/F-for-of-loop/exercise.js index 65585c6a..57d79425 100644 --- a/1-exercises/F-for-of-loop/exercise.js +++ b/1-exercises/F-for-of-loop/exercise.js @@ -8,9 +8,18 @@ let tubeStations = [ "Baker Street", "Picadilly Circus", "Oxford Street", - "Tottenham Court Road" + "Tottenham Court Road", ]; - - +for (let letter of tubeStations) { + console.log(letter); +} +console.log("_____________________"); // TODO Use a for-of loop to capitalise and output each letter in the string seperately. let str = "codeyourfuture"; +let capitalise = ""; +for (let letter of str) { + capitalise += letter.toUpperCase(); + console.log(letter.toUpperCase()); +} +console.log("_____________________"); +console.log(capitalise); \ No newline at end of file diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..175047b4 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -10,51 +10,65 @@ For example, "The temperature in London is 10 degrees" - Hint: you can call the temperatureService function from your function */ +// let usersCities = [ +// "London", +// "Paris", +// "Barcelona", +// "Dubai", +// "Mumbai", +// "São Paulo", +// "Lagos", +// ]; function getTemperatureReport(cities) { - // TODO + let city = temperatureService; + let statementTempCity = cities.map( + (element) => `The temperature in ${element} is ${city(element)} degrees` + ); + return statementTempCity; } +///////////////////////////////////////////////////////////// +// function getTemperatureReport(cities) { +// let city = temperatureService; +// return cities.forEach((element) => +// console.log(`The temperature in ${element} is ${city(element)} degrees`) +// ); +// } +// getTemperatureReport(usersCities); /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { - let temparatureMap = new Map(); - - temparatureMap.set('London', 10); - temparatureMap.set('Paris', 12); - temparatureMap.set('Barcelona', 17); - temparatureMap.set('Dubai', 27); - temparatureMap.set('Mumbai', 29); - temparatureMap.set('São Paulo', 23); - temparatureMap.set('Lagos', 33); - + let temparatureMap = new Map(); + + temparatureMap.set("London", 10); + temparatureMap.set("Paris", 12); + temparatureMap.set("Barcelona", 17); + temparatureMap.set("Dubai", 27); + temparatureMap.set("Mumbai", 29); + temparatureMap.set("São Paulo", 23); + temparatureMap.set("Lagos", 33); + return temparatureMap.get(city); } test("should return a temperature report for the user's cities", () => { - let usersCities = [ - "London", - "Paris", - "São Paulo" - ] + let usersCities = ["London", "Paris", "São Paulo"]; expect(getTemperatureReport(usersCities)).toEqual([ "The temperature in London is 10 degrees", "The temperature in Paris is 12 degrees", - "The temperature in São Paulo is 23 degrees" + "The temperature in São Paulo is 23 degrees", ]); }); test("should return a temperature report for the user's cities (alternate input)", () => { - let usersCities = [ - "Barcelona", - "Dubai" - ] + let usersCities = ["Barcelona", "Dubai"]; expect(getTemperatureReport(usersCities)).toEqual([ "The temperature in Barcelona is 17 degrees", - "The temperature in Dubai is 27 degrees" + "The temperature in Dubai is 27 degrees", ]); }); diff --git a/2-mandatory/2-retrying-random-numbers.js b/2-mandatory/2-retrying-random-numbers.js index 10aab37d..050014a0 100644 --- a/2-mandatory/2-retrying-random-numbers.js +++ b/2-mandatory/2-retrying-random-numbers.js @@ -8,11 +8,23 @@ function generateRandomNumber() { console.log("Generating number..."); return Math.round(Math.random() * 100); } +// let randomNum = generateRandomNumber; function getRandomNumberGreaterThan50() { // TODO - implement using a do-while loop -} + let i = 100; + // let randomNumber = generateRandomNumber; + do { + let random = generateRandomNumber(); + if (random > 50) { + return random; + } + i--; + } while (i > 50); +} +// console.log(getRandomNumberGreaterThan50()); +// getRandomNumberGreaterThan50(); /* ======= TESTS - DO NOT MODIFY ===== */ test("Returned value should always be greater than 50", () => { @@ -21,4 +33,4 @@ test("Returned value should always be greater than 50", () => { expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); expect(getRandomNumberGreaterThan50()).toBeGreaterThan(50); -}); +}); \ No newline at end of file diff --git a/2-mandatory/3-financial-times.js b/2-mandatory/3-financial-times.js index 2ce6fb73..7a90f246 100644 --- a/2-mandatory/3-financial-times.js +++ b/2-mandatory/3-financial-times.js @@ -1,64 +1,92 @@ +const ARTICLE_TITLES = [ + "Streaming wars drive media groups to spend more than $100bn on new content", + "Amazon Prime Video India country head: streaming is driving a TV revolution", + "Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights", + "British companies look to muscle in on US retail investing boom", + "Libor to take firm step towards oblivion on New Year's Day", + "Audit profession unattractive to new recruits, says PwC boss", + "Chinese social media users blast Elon Musk over near miss in space", + "Companies raise over $12tn in 'blockbuster' year for global capital markets", + "The three questions that dominate investment", + "Brussels urges Chile's incoming president to endorse EU trade deal", +]; /* Imagine you are working on the Financial Times web site! They have a list of article titles stored in an array. The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less. Implement the function below, which will return a new array containing only article titles which will fit. */ + function potentialHeadlines(allArticleTitles) { - // TODO + let arrLingth65 = allArticleTitles.filter( + (item) => item.split("").length <= 65 + ); + return arrLingth65; } +// console.log(potentialHeadlines(ARTICLE_TITLES)); /* 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) */ + function titleWithFewestWords(allArticleTitles) { - // TODO + let arrLingth65 = allArticleTitles.filter( + (item) => item.split("").length <= 65 + ); + return arrLingth65[arrLingth65.length - 1]; } - +// console.log(titleWithFewestWords(ARTICLE_TITLES)); /* + The editor of the FT has realised 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 + let semple = /\d+/; + let findNum = allArticleTitles.filter((item) => item.match(semple)); + return findNum; } - +// console.log(headlinesWithNumbers(ARTICLE_TITLES)); /* + 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. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + // let everage = allArticleTitles.map(el=>el.length) + let average = allArticleTitles.join("").length / allArticleTitles.length; + return Math.floor(average); } - - +// console.log(averageNumberOfCharacters(ARTICLE_TITLES)); /* ======= List of Articles - DO NOT MODIFY ===== */ -const ARTICLE_TITLES = [ - "Streaming wars drive media groups to spend more than $100bn on new content", - "Amazon Prime Video India country head: streaming is driving a TV revolution", - "Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights", - "British companies look to muscle in on US retail investing boom", - "Libor to take firm step towards oblivion on New Year's Day", - "Audit profession unattractive to new recruits, says PwC boss", - "Chinese social media users blast Elon Musk over near miss in space", - "Companies raise over $12tn in 'blockbuster' year for global capital markets", - "The three questions that dominate investment", - "Brussels urges Chile's incoming president to endorse EU trade deal", -]; +// const ARTICLE_TITLES = [ +// "Streaming wars drive media groups to spend more than $100bn on new content", +// "Amazon Prime Video India country head: streaming is driving a TV revolution", +// "Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights", +// "British companies look to muscle in on US retail investing boom", +// "Libor to take firm step towards oblivion on New Year's Day", +// "Audit profession unattractive to new recruits, says PwC boss", +// "Chinese social media users blast Elon Musk over near miss in space", +// "Companies raise over $12tn in 'blockbuster' year for global capital markets", +// "The three questions that dominate investment", +// "Brussels urges Chile's incoming president to endorse EU trade deal", +// ]; /* ======= TESTS - DO NOT MODIFY ===== */ test("should only return potential headlines", () => { - expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual(new Set([ - "British companies look to muscle in on US retail investing boom", - "Libor to take firm step towards oblivion on New Year's Day", - "Audit profession unattractive to new recruits, says PwC boss", - "The three questions that dominate investment" - ])); + expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual( + new Set([ + "British companies look to muscle in on US retail investing boom", + "Libor to take firm step towards oblivion on New Year's Day", + "Audit profession unattractive to new recruits, says PwC boss", + "The three questions that dominate investment", + ]) + ); }); test("should return an empty array for empty input", () => { @@ -66,16 +94,27 @@ test("should return an empty array for empty input", () => { }); test("should return the title with the fewest words", () => { - expect(titleWithFewestWords(ARTICLE_TITLES)).toEqual("The three questions that dominate investment"); + expect(titleWithFewestWords(ARTICLE_TITLES)).toEqual( + "The three questions that dominate investment" + ); }); test("should only return headlines containing numbers", () => { - expect(new Set(headlinesWithNumbers(ARTICLE_TITLES))).toEqual(new Set([ - "Streaming wars drive media groups to spend more than $100bn on new content", - "Companies raise over $12tn in 'blockbuster' year for global capital markets" - ])); + expect(new Set(headlinesWithNumbers(ARTICLE_TITLES))).toEqual( + new Set([ + "Streaming wars drive media groups to spend more than $100bn on new content", + "Companies raise over $12tn in 'blockbuster' year for global capital markets", + ]) + ); }); test("should return the average number of characters in a headline", () => { expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65); }); + +//💫 💫 💫 +//This logic returns the last headline in ARTICLE_TITLES which has 65 characters +// or less - by co-incidence this is also the headline with the fewest words so your +// test passes. But if the order of ARTICLE_TITLES is changed, the test will fail. +// If you have time, try to re-implement this with the correct logic. +//💫 💫 💫 \ No newline at end of file diff --git a/2-mandatory/4-stocks.js b/2-mandatory/4-stocks.js index 72d62f94..794f2cdd 100644 --- a/2-mandatory/4-stocks.js +++ b/2-mandatory/4-stocks.js @@ -12,10 +12,10 @@ const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"]; const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ [179.19, 180.33, 176.28, 175.64, 172.99], // AAPL - [340.69, 342.45, 334.69, 333.20, 327.29], // MSFT + [340.69, 342.45, 334.69, 333.2, 327.29], // MSFT [3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN - [2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL - [1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA + [2951.88, 2958.13, 2938.33, 2928.3, 2869.45], // GOOGL + [1101.3, 1093.94, 1067.0, 1008.87, 938.53], // TSLA ]; /* @@ -33,9 +33,26 @@ 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! */ + function getAveragePrices(closingPricesForAllStocks) { - // TODO + // let arr = []; + let averagePrices = closingPricesForAllStocks + .map((el) => { + // console.log(el); + return ( + el.reduce((acc, value, _, { length }) => { + // console.log(length); + //the _ refers to the number of times the reducer has looped over the array. + // I use _ because I don't neded so by use _ I say just ignore this option + return acc + value; + }) / el.length + ); + }) + .map((el) => Number(el.toFixed(2))); + + return averagePrices; } +// console.log(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)); /* We also want to see what the change in price is from the first day to the last day for each stock. @@ -48,8 +65,12 @@ 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 PriceChanges = closingPricesForAllStocks.map((el) => + Number((el[el.length - 1] - el[0]).toFixed(2).replace(/(\.0+|0+)$/, "")) + ); + return PriceChanges; } +// console.log(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)); /* As part of a financial report, we want to see what the highest price was for each stock in the last 5 days. @@ -64,31 +85,42 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let capitalisedSTocks = stocks.map((el) => el.toUpperCase()); + let highestPrice = closingPricesForAllStocks.map((el) => + Math.max(...el).toFixed(2) + ); + let result = []; + for (let i = 0; i < stocks.length; i++) { + result.push( + `The highest price of ${capitalisedSTocks[i]} in the last 5 days was ${highestPrice[i]}` + ); + } + return result; } - +console.log( + highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS) +); +// highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS); /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the average price for each stock", () => { - expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( - [176.89, 335.66, 3405.66, 2929.22, 1041.93] - ); + expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([ + 176.89, 335.66, 3405.66, 2929.22, 1041.93, + ]); }); test("should return the price change for each stock", () => { - expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( - [-6.2, -13.4, 23.9, -82.43, -162.77] - ); + expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([-6.2, -13.4, 23.9, -82.43, -162.77, ]); }); test("should return a description of the highest price for each stock", () => { - expect(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)).toEqual( - [ - "The highest price of AAPL in the last 5 days was 180.33", - "The highest price of MSFT in the last 5 days was 342.45", - "The highest price of AMZN in the last 5 days was 3421.37", - "The highest price of GOOGL in the last 5 days was 2958.13", - "The highest price of TSLA in the last 5 days was 1101.30" - ] - ); -}); + expect( + highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS) + ).toEqual([ + "The highest price of AAPL in the last 5 days was 180.33", + "The highest price of MSFT in the last 5 days was 342.45", + "The highest price of AMZN in the last 5 days was 3421.37", + "The highest price of GOOGL in the last 5 days was 2958.13", + "The highest price of TSLA in the last 5 days was 1101.30", + ]); +}); \ No newline at end of file diff --git a/3-extra/1-factorial.js b/3-extra/1-factorial.js index 31f8052c..714356c1 100644 --- a/3-extra/1-factorial.js +++ b/3-extra/1-factorial.js @@ -9,9 +9,13 @@ */ function factorial(input) { - // TODO + let result = 1; + for (let i = 1; i <= input; i++) { + result *= i; + } + return result; } - +// console.log(factorial(5)); /* ======= TESTS - DO NOT MODIFY ===== */ test("3! should be 6", () => { @@ -24,4 +28,4 @@ test("5! should be 120", () => { test("10! should be 3628800", () => { expect(factorial(10)).toEqual(3628800); -}); +}); \ No newline at end of file diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..e707b248 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -10,73 +10,119 @@ Each title in the resulting array should be the highest rated book in its genre. */ -function getHighestRatedInEachGenre(books) { - // TODO -} - - +// function getHighestRatedInEachGenre(books) { +// let allBooks = books.map((books) => Object.assign(books)); +// return allBooks; +// } +// console.log(getHighestRatedInEachGenre(BOOKS)); /* ======= Book data - DO NOT MODIFY ===== */ -const BOOKS = [ - { +const BOOKS = [{ title: "The Lion, the Witch and the Wardrobe", genre: "children", - rating: 4.7 + rating: 4.7, }, { title: "Sapiens: A Brief History of Humankind", genre: "non-fiction", - rating: 4.7 + rating: 4.7, }, { title: "Nadiya's Fast Flavours", genre: "cooking", - rating: 4.7 + rating: 4.7, }, { title: "Harry Potter and the Philosopher's Stone", genre: "children", - rating: 4.8 + rating: 4.8, }, { title: "A Life on Our Planet", genre: "non-fiction", - rating: 4.8 + rating: 4.8, }, { title: "Dishoom: The first ever cookbook from the much-loved Indian restaurant", genre: "cooking", - rating: 4.85 + rating: 4.85, }, { title: "Gangsta Granny Strikes Again!", genre: "children", - rating: 4.9 + rating: 4.9, }, { title: "Diary of a Wimpy Kid", genre: "children", - rating: 4.6 + rating: 4.6, }, { title: "BOSH!: Simple recipes. Unbelievable results. All plants.", genre: "cooking", - rating: 4.6 + rating: 4.6, }, { title: "The Book Your Dog Wishes You Would Read", genre: "non-fiction", - rating: 4.85 + rating: 4.85, }, -] +]; + +// function category(books, genre) { +// let children = []; +// books.forEach((element) => { +// if (element.genre === genre) { +// children.push(element); +// } +// }); +// return children; +// } + +// function operation(books, genre) { +// let children = category(books, genre); +// let result = children.sort((a, b) => b.rating - a.rating)[0]; +// return result.title; +// } +// function getHighestRatedInEachGenre(books) { +// let children = operation(books, "children"); +// let non_fiction = operation(books, "non-fiction"); +// let cooking = operation(books, "cooking"); +// const result = [children, non_fiction, cooking]; +// return result; +// } +// console.log(getHighestRatedInEachGenre(BOOKS)); +// getHighestRatedInEachGenre(BOOKS); +function arranGeenre(books, category) { + const children = []; + + books.filter((value, index) => { + if (value.genre === category) { + children.push(value); + } + }); + let result = children.sort((a, b) => b.rating - a.rating); + return result[0].title; +} + +//💫 💫 💫 💫 💫 💫 💫 💫 💫 💫 + +function getHighestRatedInEachGenre(books) { + let children = arranGeenre(books, "children"); + let non_fiction = arranGeenre(books, "non-fiction"); + let cooking = arranGeenre(books, "cooking"); + const result = [children, non_fiction, cooking]; + return result; +} +console.log(getHighestRatedInEachGenre(BOOKS)); /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the highest rated book in each genre", () => { - expect(new Set(getHighestRatedInEachGenre(BOOKS))).toEqual(new Set( - [ + 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" - ] - )); + "Dishoom: The first ever cookbook from the much-loved Indian restaurant", + ]) + ); }); \ No newline at end of file diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..85603163 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,24 +14,27 @@ */ function generateFibonacciSequence(n) { - // TODO -} + let result = [0, 1]; + for (let i = 2; i < n; i++) { + result[i] = result[i - 2] + result[i - 1]; + } + return result; +} +// console.log(generateFibonacciSequence(10)); /* ======= 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] - ); + 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 diff --git a/package.json b/package.json index dc4e9ace..0c49586a 100644 --- a/package.json +++ b/package.json @@ -1,27 +1,29 @@ { - "name": "javascript-core-1-coursework-week3", - "version": "1.0.0", - "description": "Exercises for JS1 Week 3", - "license": "CC-BY-SA-4.0", - "scripts": { - "test": "jest --testRegex='mandatory[/\\\\].*\\.js$'", - "extra-tests": "jest --testRegex='extra[/\\\\].*\\.js$'" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week3.git" - }, - "bugs": { - "url": "https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week3/issues" - }, - "jest": { - "reporters": [ - "default", - "/util/github-action-reporter.js" - ] - }, - "homepage": "https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week3#readme", - "devDependencies": { - "jest": "^26.6.3" - } -} + "name": "javascript-core-1-coursework-week3", + "version": "1.0.0", + "description": "Exercises for JS1 Week 3", + "license": "CC-BY-SA-4.0", + "scripts": { + "test": "jest --testRegex='mandatory[/\\\\].*\\.js$'", + "extra-tests": "jest --testRegex='extra[/\\\\].*\\.js$'", + "dev": "nodemon 3-extra/2-array-of-objects.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week3.git" + }, + "bugs": { + "url": "https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week3/issues" + }, + "jest": { + "reporters": [ + "default", + "/util/github-action-reporter.js" + ] + }, + "homepage": "https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week3#readme", + "devDependencies": { + "jest": "^26.6.3", + "nodemon": "^2.0.20" + } +} \ No newline at end of file