diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..0062c63b 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 diff --git a/1-exercises/C-array-get-set/exercise.js b/1-exercises/C-array-get-set/exercise.js index 5ca911d5..3e97995e 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..36d9c411 100644 --- a/1-exercises/C-array-get-set/exercises2.js +++ b/1-exercises/C-array-get-set/exercises2.js @@ -7,7 +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..1809a952 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.js @@ -25,7 +25,9 @@ const AGES = [ 63, 49 ]; - +for (let i = 0; i < WRITERS.length; i++) { + console.log(`${WRITERS[i]} is ${AGES[i]} years old`); +} // TODO - Write for loop code here /* diff --git a/1-exercises/E-while-loop-with-array/exercise.js b/1-exercises/E-while-loop-with-array/exercise.js index d584cd75..880a4591 100644 --- a/1-exercises/E-while-loop-with-array/exercise.js +++ b/1-exercises/E-while-loop-with-array/exercise.js @@ -18,6 +18,14 @@ const BIRTHDAYS = [ function findFirstJulyBDay(birthdays) { // TODO + let i = 0; + while (i < birthdays.length) { + const birthday = birthdays[i]; + if (birthday.includes("July")) { + return birthday; + } + i++; + } } console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..93d69a40 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,6 +13,15 @@ function getTemperatureReport(cities) { // TODO + const temperatureReport = []; + + for(let i = 0; i < cities.length; i++) { + const temperature = temperatureService(cities[i]); + + temperatureReport.push(`The temperature in ${cities[i]} is ${temperature} degrees`); + } + + return temperatureReport; } diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..867832d0 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -6,15 +6,26 @@ */ function potentialHeadlines(allArticleTitles) { // TODO + const headTitle = allArticleTitles.filter(title => title.length < 65); + return headTitle; } /* 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) + (you can assume words will always be separated by a space) */ function titleWithFewestWords(allArticleTitles) { // TODO + let shortestTitle = allArticleTitles[0]; + + for (let i = 0; i < allArticleTitles.length; i++) { + const currentTitle = allArticleTitles[i]; + if (currentTitle.length < shortestTitle.length) { + shortestTitle = currentTitle + } + } + return shortestTitle; } /* @@ -24,6 +35,7 @@ function titleWithFewestWords(allArticleTitles) { */ function headlinesWithNumbers(allArticleTitles) { // TODO + return allArticleTitles.filter(title => /\d/.test(title)); } /* @@ -32,6 +44,9 @@ function headlinesWithNumbers(allArticleTitles) { */ function averageNumberOfCharacters(allArticleTitles) { // TODO + const articleSum = allArticleTitles.reduce((acc, val) => acc + val.length, 0); + return Math.round(articleSum / allArticleTitles.length) + } diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..768cde34 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -35,6 +35,14 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ */ function getAveragePrices(closingPricesForAllStocks) { // TODO + let currentStock = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + const numberOfStocks = closingPricesForAllStocks[i].length; + const sumOfCurrentStock = closingPricesForAllStocks[i].reduce((accumulator, currentValue) => accumulator + currentValue, 0); + const roundedStock = Math.round((sumOfCurrentStock / numberOfStocks) * 100) / 100; + currentStock.push(roundedStock); + } + return currentStock; } /* @@ -49,6 +57,17 @@ function getAveragePrices(closingPricesForAllStocks) { */ function getPriceChanges(closingPricesForAllStocks) { // TODO + let closingPrices = []; + + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + const numberOfPrices = closingPricesForAllStocks[i].length - 1; + const firstDay = closingPricesForAllStocks[i].at(0); + const lastDay = closingPricesForAllStocks[i].at(numberOfPrices); + const priceChange = Math.round((lastDay - firstDay) * 100) / 100; + + closingPrices.push(priceChange); + } + return closingPrices; } /* @@ -65,6 +84,17 @@ function getPriceChanges(closingPricesForAllStocks) { */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { // TODO + let allHighestPrices = []; + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + const stockName = stocks[i].toUpperCase(); + const stockPrices = closingPricesForAllStocks[i]; + const highestPrice = Math.max(...stockPrices).toFixed(2); + + allHighestPrices.push(`The highest price of ${stockName} in the last 5 days was ${highestPrice}`) + + } + + return allHighestPrices; } diff --git a/3-extra/1-radio-stations.js b/3-extra/1-radio-stations.js index 577076e9..8c244328 100644 --- a/3-extra/1-radio-stations.js +++ b/3-extra/1-radio-stations.js @@ -14,7 +14,16 @@ */ // `getAllFrequencies` goes here +function getAllFrequencies() { + const allFrequencies = [ + 87, 88, 89, 90, 91, 92, + 93, 94, 95, 96, 97, 98, + 99, 100, 101, 102, 103, + 104, 105, 106, 107, 108, + ] + return allFrequencies; +} /** * Next, let's write a function that gives us only the frequencies that are radio stations. * Call this function `getStations`. @@ -25,7 +34,11 @@ * - Return only the frequencies that are radio stations. */ // `getStations` goes here - +function getStations() { + const allFrequencies = getAllFrequencies(); + const stations = allFrequencies.filter(isRadioStation); + return stations; +} /* * ======= TESTS - DO NOT MODIFY ======= * Note: You are not expected to understand everything below this comment! diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..b028ab7f 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -12,6 +12,18 @@ function getHighestRatedInEachGenre(books) { // TODO + const highestRatedBooks = {}; + for (const book of books) { + const { title, genre, rating } = book; + + if (highestRatedBooks[genre] === undefined || rating > highestRatedBooks[genre].rating) { + highestRatedBooks[genre] = { title, rating }; + } + } + const highestRatedTitlesByGenre = Object.values(highestRatedBooks).map(book => book.title); + + return highestRatedTitlesByGenre; + } diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..fac6f97b 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -15,6 +15,13 @@ function generateFibonacciSequence(n) { // TODO + const sequence = [0, 1]; + + for (let i = 2; i < n; i++) { + sequence.push(sequence[i - 1] + sequence[i - 2]); + } + + return sequence; } /* ======= TESTS - DO NOT MODIFY ===== */