From b53ee9a20f712c4c3ab2a38005fad0c8f15ffa30 Mon Sep 17 00:00:00 2001 From: Olha Danylevska Date: Fri, 26 May 2023 12:02:18 +0100 Subject: [PATCH 1/3] 1 exercises --- 1-exercises/A-undefined/exercise.js | 24 ++++++++++++++++++- 1-exercises/B-array-literals/exercise.js | 8 +++++-- 1-exercises/C-array-get-set/exercise.js | 4 ++-- 1-exercises/C-array-get-set/exercises2.js | 1 + 1-exercises/D-for-loop/exercise.js | 6 +++++ .../E-while-loop-with-array/exercise.js | 9 ++++++- 6 files changed, 46 insertions(+), 6 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..20b82ef9 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -13,12 +13,23 @@ let a; console.log(a); +//because "a" did not assign to any value +// let a = 2; +// console.log(a) + // Example 2 function sayHello() { let message = "Hello"; } +//I think because 'message' is declared but its value is never read. + +// function sayHello() { +// let message = "Hello"; +// return message +// } + let hello = sayHello(); console.log(hello); @@ -28,9 +39,20 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } + sayHelloToUser(); +// any returned value +// function sayHelloToUser(user) { +// let userName = user +// console.log(`Hello ${userName}`); +// return `Hello ${userName}` +// } + + // Example 4 -let arr = [1,2,3]; +let arr = [1, 2, 3]; console.log(arr[3]); + +//non-existing array element diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..5cd0aa51 100644 --- a/1-exercises/B-array-literals/exercise.js +++ b/1-exercises/B-array-literals/exercise.js @@ -4,8 +4,8 @@ Declare some variables assigned to arrays of values */ -let numbers = []; // add numbers from 1 to 10 into this array -let mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares +let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // add numbers from 1 to 10 into this array +let mentors = ["Daniel", "Irina", "Rares"]; // Create an array with the names of the mentors: Daniel, Irina and Rares /* DO NOT EDIT BELOW THIS LINE @@ -19,3 +19,7 @@ console.log(mentors); [1,2,3,4,5,6,7,8,9,10] ['Daniel', 'Irina', 'Rares'] */ + + + + diff --git a/1-exercises/C-array-get-set/exercise.js b/1-exercises/C-array-get-set/exercise.js index 5ca911d5..9c1d01d7 100644 --- a/1-exercises/C-array-get-set/exercise.js +++ b/1-exercises/C-array-get-set/exercise.js @@ -5,11 +5,11 @@ */ function first(arr) { - return; // complete this statement + return arr[0]; // complete this statement } function last(arr) { - return; // complete this statement + return arr[arr.length - 1]; // complete this statement } /* diff --git a/1-exercises/C-array-get-set/exercises2.js b/1-exercises/C-array-get-set/exercises2.js index 6b6b007a..f9095792 100644 --- a/1-exercises/C-array-get-set/exercises2.js +++ b/1-exercises/C-array-get-set/exercises2.js @@ -7,6 +7,7 @@ */ let numbers = [1, 2, 3]; // Don't change this array literal declaration +numbers.push(4) /* DO NOT EDIT BELOW THIS LINE diff --git a/1-exercises/D-for-loop/exercise.js b/1-exercises/D-for-loop/exercise.js index 081002b2..7221c4f0 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.js @@ -28,6 +28,12 @@ const AGES = [ // 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/E-while-loop-with-array/exercise.js b/1-exercises/E-while-loop-with-array/exercise.js index d584cd75..d8d6ea21 100644 --- a/1-exercises/E-while-loop-with-array/exercise.js +++ b/1-exercises/E-while-loop-with-array/exercise.js @@ -17,7 +17,14 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + let i = 0 + while (i < birthdays.length) { + if (birthdays[i].includes("July")) { + return birthdays[i] + } + i++ + } } + console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" From 5cc3ba3fefb35cea333943bddad9ece7119e34a8 Mon Sep 17 00:00:00 2001 From: Olha Danylevska Date: Fri, 26 May 2023 12:04:02 +0100 Subject: [PATCH 2/3] 2 mandatory --- 2-mandatory/1-weather-report.js | 17 ++++++++++-- 2-mandatory/2-financial-times.js | 42 ++++++++++++++++++++++++++-- 2-mandatory/3-stocks.js | 47 ++++++++++++++++++++++++++++---- 3 files changed, 96 insertions(+), 10 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..392958b5 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,13 +13,21 @@ function getTemperatureReport(cities) { // TODO + const reports = []; + + + for (item of cities) { + reports.push(`The temperature in ${item} is ${temperatureService(item)} degrees`) + } + + return reports } /* ======= TESTS - DO NOT MODIFY ===== */ function temperatureService(city) { - let temparatureMap = new Map(); + let temparatureMap = new Map(); temparatureMap.set('London', 10); temparatureMap.set('Paris', 12); @@ -28,10 +36,15 @@ function temperatureService(city) { temparatureMap.set('Mumbai', 29); temparatureMap.set('São Paulo', 23); temparatureMap.set('Lagos', 33); - + return temparatureMap.get(city); } +test("test should return array the same length as an argument", () => { + let usersCities = ["London", "Paris", "São Paulo"]; + expect(getTemperatureReport(usersCities).length).toEqual(3); +}); + test("should return a temperature report for the user's cities", () => { let usersCities = [ "London", diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..90965181 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,6 +5,13 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { + let articlesWhichFit = [] + for (item of allArticleTitles) { + if (item.length < 65) { + articlesWhichFit.push(item) + } + } + return articlesWhichFit // TODO } @@ -14,7 +21,15 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let smallest = allArticleTitles[0] + for (i = 0; i < allArticleTitles.length; i++) { + if (smallest.length > allArticleTitles[i].length) { + smallest = allArticleTitles[i] + } + } + + return smallest + } /* @@ -23,7 +38,17 @@ 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 = [] + for (item of allArticleTitles) { + for (element of item) { + element = parseInt(element) + if (item.includes(element)) { + numberArray.push(item) + break + } + } + } + return numberArray; } /* @@ -31,11 +56,22 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + sumOfStrings = [] + for (item of allArticleTitles) { + let itemLength = item.length + sumOfStrings.push(itemLength) + } + const initialValue = 0; + const sumWithInitial = sumOfStrings.reduce( + (accumulator, currentValue) => accumulator + currentValue, + initialValue + ); + return Math.floor(Math.round(sumWithInitial / allArticleTitles.length)) } + /* ======= List of Articles - DO NOT MODIFY ===== */ const ARTICLE_TITLES = [ "Streaming wars drive media groups to spend more than $100bn on new content", diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..224b65a8 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -8,7 +8,7 @@ */ /* ======= Stock data - DO NOT MODIFY ===== */ -const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"]; +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 @@ -34,9 +34,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + arrayOfPrices = [] + for (item of closingPricesForAllStocks) { + const initialValue = 0; + const sumWithInitial = item.reduce( + (accumulator, currentValue) => accumulator + currentValue, + initialValue + ); + let averagePrice = sumWithInitial / item.length + let averagePriceDecimal = Math.round(averagePrice * 100) / 100 + arrayOfPrices.push(averagePriceDecimal) + } + return arrayOfPrices } + /* 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 @@ -48,9 +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 eachArrayOfDifference = [] + for (element of closingPricesForAllStocks) { + for (i = 0; i < element.length; i++) { + let lastPrice = element.slice(-1) + let difference = lastPrice - element[i] + let differenceWithDecimal = Math.round(difference * 100) / 100 + eachArrayOfDifference.push(differenceWithDecimal) + break + } + } + return eachArrayOfDifference } - /* As part of a financial report, we want to see what the highest price was for each stock in the last 5 days. Implement the below function, which @@ -64,7 +85,23 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + let arrayOfAnswers = [] + let arrayOfStrings = [] + for (item of closingPricesForAllStocks) { + for (i = 0; i < item.length; i++) + if (item[0] < item[i]) { + item[0] = item[i] + } + let answer = item[0] + answer = answer.toFixed(2) + console.log("ANSWER", answer) + arrayOfAnswers.push(answer) + } + for (i = 0; i < arrayOfAnswers.length; i++) { + arrayOfStrings.push(`The highest price of ${stocks[i]} in the last 5 days was ${arrayOfAnswers[i]}`) + } + + return arrayOfStrings } From 0a37b843585e88b96c2eb08d560d445f292fa01a Mon Sep 17 00:00:00 2001 From: Olha Danylevska Date: Fri, 26 May 2023 12:06:08 +0100 Subject: [PATCH 3/3] extra --- 3-extra/1-radio-stations.js | 29 +++++++++++++++++++++ 3-extra/2-array-of-objects.js | 48 ++++++++++++++++++++++++++++++++++- 3-extra/3-fibonacci.js | 23 +++++++++++++++-- 3 files changed, 97 insertions(+), 3 deletions(-) diff --git a/3-extra/1-radio-stations.js b/3-extra/1-radio-stations.js index 577076e9..c79c3709 100644 --- a/3-extra/1-radio-stations.js +++ b/3-extra/1-radio-stations.js @@ -13,11 +13,26 @@ * - Should return this array to use in other functions */ + // `getAllFrequencies` goes here +function getAllFrequencies() { + let startFrequency = 87 + let arrayOfFrequencies = [] + while (startFrequency <= 108) { + arrayOfFrequencies.push(startFrequency) + startFrequency = startFrequency + 1 + } + return arrayOfFrequencies +} + + + /** * Next, let's write a function that gives us only the frequencies that are radio stations. * Call this function `getStations`. + * + * * This function should: * - Get the available frequencies from `getAllFrequencies` @@ -26,6 +41,17 @@ */ // `getStations` goes here + +function getStations(allFrequencies) { + let arrayOfRadioStations = [] + allFrequencies = getAllFrequencies() + for (i = 0; i < allFrequencies.length; i++) { + if (isRadioStation(allFrequencies[i])) { + arrayOfRadioStations.push(allFrequencies[i]) + } + } + return arrayOfRadioStations +} /* * ======= TESTS - DO NOT MODIFY ======= * Note: You are not expected to understand everything below this comment! @@ -55,6 +81,9 @@ function isRadioStation(frequency) { return getAvailableStations().includes(frequency); } + + + test("getAllFrequencies() returns all frequencies between 87 and 108", () => { expect(getAllFrequencies()).toEqual([ 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..ba175d38 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,10 +11,56 @@ */ function getHighestRatedInEachGenre(books) { - // TODO + let arrOfCookingBooks = [] + let arrOfChildrenBooks = [] + let arrayOfNonFictionBooks = [] + let result = [] + + let cookingBook = books.map(cookingBook => { + if (cookingBook.genre === "cooking") { + arrOfCookingBooks.push(cookingBook) + } + }); + let bestCookingBook = arrOfCookingBooks.reduce( + (b1, b2) => { + return b1.rating > b2.rating ? b1 : b2 + } + ); + + result.push(bestCookingBook.title) + + let childrenBook = books.map(childrenBook => { + if (childrenBook.genre === "children") { + arrOfChildrenBooks.push(childrenBook) + } + }); + let bestCildrenBook = arrOfChildrenBooks.reduce( + (b1, b2) => { + return b1.rating > b2.rating ? b1 : b2 + } + ); + + result.push(bestCildrenBook.title) + + let nonFictionBook = books.map(nonFictionBook => { + if (nonFictionBook.genre === "non-fiction") { + arrayOfNonFictionBooks.push(nonFictionBook) + } + }); + let bestNonFictionBook = arrayOfNonFictionBooks.reduce( + (b1, b2) => { + return b1.rating > b2.rating ? b1 : b2 + } + ); + + result.push(bestNonFictionBook.title) + + return result + } + /* ======= Book data - DO NOT MODIFY ===== */ const BOOKS = [ { diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..1d362996 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,10 +14,29 @@ */ function generateFibonacciSequence(n) { - // TODO + var arrayOfNumbers = [0, 1] + + while (arrayOfNumbers.length != n) { + let number = arrayOfNumbers[arrayOfNumbers.length - 1] + arrayOfNumbers[arrayOfNumbers.length - 2] + arrayOfNumbers.push(number) + + } + console.log(arrayOfNumbers) + return arrayOfNumbers } -/* ======= TESTS - DO NOT MODIFY ===== */ + + + + + + +generateFibonacciSequence(5) + + + + +// * ======= 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]