diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..8d2195bf 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); +//a isn't given a value. // Example 2 @@ -21,6 +22,7 @@ function sayHello() { let hello = sayHello(); console.log(hello); +//the function doesn't return anything. // Example 3 @@ -29,8 +31,10 @@ function sayHelloToUser(user) { } sayHelloToUser(); +//Nothing has been written inside the parantheses when calling the function. // Example 4 let arr = [1,2,3]; console.log(arr[3]); +//the array holds values from 0-2, so 3 doesn't exist. diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..5bec732c 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..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..fdd67033 100644 --- a/1-exercises/C-array-get-set/exercises2.js +++ b/1-exercises/C-array-get-set/exercises2.js @@ -6,8 +6,9 @@ - change the first value in the array to the number 1 */ -let numbers = [1, 2, 3]; // Don't change this array literal declaration - +let numbers = [1, 2, 3]; +numbers.push(4);// Don't change this array literal declaration +numbers[0] = 1; /* 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..789e3665 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.js @@ -10,21 +10,9 @@ Using a for loop, output to the console a line about the age of each writer. */ -const WRITERS = [ - "Virginia Woolf", - "Zadie Smith", - "Jane Austen", - "Bell Hooks", - "Yukiko Motoya" -] - -const AGES = [ - 59, - 40, - 41, - 63, - 49 -]; +const WRITERS = ["Virginia Woolf", "Zadie Smith", "Jane Austen", "Bell Hooks", "Yukiko Motoya"] +const AGES = [59, 40, 41, 63, 49]; +for (i = 0, i < ) // 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..e191cd84 100644 --- a/1-exercises/E-while-loop-with-array/exercise.js +++ b/1-exercises/E-while-loop-with-array/exercise.js @@ -17,7 +17,10 @@ const BIRTHDAYS = [ ]; function findFirstJulyBDay(birthdays) { - // TODO + while(i <= BIRTHDAYS.length) { + if (array[i] === July) { + return i; + } } -console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" +console.log(findFirstJulyBDay(BIRTHDAYS)); } \ No newline at end of file diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..9706d1e2 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,7 +12,14 @@ */ 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..240dd92c 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,16 +5,33 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO -} + const potentialHeadlines = []; + + for (let i = 0; i < allArticleTitles.length; i++) { + const articleTitle = allArticleTitles[i]; + + if (articleTitle.length <= 65) { + potentialHeadlines.push(articleTitle); + } + } + return potentialHeadlines; +} + /* 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 shortestTitle = allArticleTitles[0]; + + for (let i=1; i acc + cur); + + // Calculate the average closing price for the current stock + const avg = sum / stockPrices.length; + + // Round the average closing price to 2 decimal places and add it to the array + averagePrices.push(parseFloat(avg.toFixed(2))); + } + return averagePrices; +} /* 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,7 +66,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 + const priceChanges = []; + + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + const prices = closingPricesForAllStocks[i]; + const priceChange = (prices[prices.length-1] - prices[0]).toFixed(2); + priceChanges.push(parseFloat(priceChange)); + } + + return priceChanges; } /* @@ -64,7 +90,16 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + const priceDescriptions = []; + + for (let i = 0; i < stocks.length; i++) { + const ticker = stocks[i].toUpperCase(); + const highestPrice = Math.max(...closingPricesForAllStocks[i]).toFixed(2); + priceDescriptions.push(`The highest price of ${ticker} in the last 5 days was ${highestPrice}`); + + } + + return priceDescriptions; } diff --git a/3-extra/1-radio-stations.js b/3-extra/1-radio-stations.js index 577076e9..db031027 100644 --- a/3-extra/1-radio-stations.js +++ b/3-extra/1-radio-stations.js @@ -31,28 +31,20 @@ * Note: You are not expected to understand everything below this comment! */ -function getAvailableStations() { - // Using `stations` as a property as defining it as a global variable wouldn't - // always make it initialized before the function is called - if (!getAvailableStations.stations) { - const stationCount = 4; - getAvailableStations.stations = []; - while (getAvailableStations.stations.length < stationCount) { - let randomFrequency = Math.floor(Math.random() * (108 - 87 + 1) + 87); - if (!getAvailableStations.stations.includes(randomFrequency)) { - getAvailableStations.stations.push(randomFrequency); - } - } - getAvailableStations.stations.sort(function (frequencyA, frequencyB) { - return frequencyA - frequencyB; - }); +function getAllFrequencies() { + const frequencies = []; + for (let i = 87; i <= 108; i++) { + frequencies.push(i); } - - return getAvailableStations.stations; + return frequencies; } -function isRadioStation(frequency) { - return getAvailableStations().includes(frequency); +function getStations() { + const allFrequencies = getAllFrequencies(); + const radioStations = allFrequencies.filter((frequency) => { + return isRadioStation(frequency); + }); + return radioStations; } test("getAllFrequencies() returns all frequencies between 87 and 108", () => { diff --git a/3-extra/2-array-of-objects.js b/3-extra/2-array-of-objects.js index ee57960f..fe56f13c 100644 --- a/3-extra/2-array-of-objects.js +++ b/3-extra/2-array-of-objects.js @@ -11,9 +11,21 @@ */ function getHighestRatedInEachGenre(books) { - // TODO -} + // this object will store the highest rated books for each genre + const highestRated = {}; + + // I'll loop through the books array and update the highest rated book for each genre + books.forEach(book => { + if (!highestRated[book.genre] || book.rating > highestRated[book.genre].rating) { + highestRated[book.genre] = book; + } + }); + // this will return an array of book titles from the highest rated books object + const titles = Object.values(highestRated).map(book => book.title); + + return titles; +} /* ======= Book data - DO NOT MODIFY ===== */ const BOOKS = [ diff --git a/3-extra/3-fibonacci.js b/3-extra/3-fibonacci.js index 9ef9aec7..c64aa38d 100644 --- a/3-extra/3-fibonacci.js +++ b/3-extra/3-fibonacci.js @@ -14,7 +14,13 @@ */ function generateFibonacciSequence(n) { - // TODO + let fibonacciSequence = [0, 1]; + + for (let i = 2; i < n; i++) { + fibonacciSequence[i] = fibonacciSequence[i - 1] + fibonacciSequence[i - 2]; + } + + return fibonacciSequence; } /* ======= TESTS - DO NOT MODIFY ===== */