diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..3bd82b83 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -11,7 +11,7 @@ // Example 1 let a; -console.log(a); +console.log(a); //value is not asigned // Example 2 @@ -20,7 +20,7 @@ function sayHello() { } let hello = sayHello(); -console.log(hello); +console.log(hello); //sayHello() function does not have a return statement // Example 3 @@ -28,9 +28,9 @@ function sayHelloToUser(user) { console.log(`Hello ${user}`); } -sayHelloToUser(); +sayHelloToUser(); //sayHelloToUser() function is not given a value when the function is called // Example 4 let arr = [1,2,3]; -console.log(arr[3]); +console.log(arr[3]); //array contains only 3 elements and indices like 0,1,2 when we write arr[3] it means that we whant to reach the element number 4 which does not exist here. diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..14ca832c 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..01a088a0 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..4b94ad51 100644 --- a/1-exercises/C-array-get-set/exercises2.js +++ b/1-exercises/C-array-get-set/exercises2.js @@ -7,7 +7,8 @@ */ let numbers = [1, 2, 3]; // Don't change this array literal declaration - +numbers.push(4); +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..1d36683a 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.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/E-while-loop-with-array/exercise.js b/1-exercises/E-while-loop-with-array/exercise.js index d584cd75..c5052c8c 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){ + if(BIRTHDAYS[i].includes("July")){ + return BIRTHDAYS[i]; + } + 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..591dcd67 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -13,6 +13,14 @@ function getTemperatureReport(cities) { // TODO + let forecast = []; + for(let i = 0; i < cities.length; i++){ + const currentCity = cities[i] + const temperature = temperatureService(cities[i]); + const report = `The temperature in ${currentCity} is ${temperature} degrees`; + forecast.push(report); + } + return forecast; } diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..3c46b85a 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -5,7 +5,13 @@ Implement the function below, which will return a new array containing only article titles which will fit. */ function potentialHeadlines(allArticleTitles) { - // TODO + const titles = []; + for(let i = 0; i < allArticleTitles.length; i++){ + if(allArticleTitles[i].length <= 65){ + titles.push(allArticleTitles[i]) + } + } + return titles; } /* @@ -14,7 +20,15 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + let fewestSoFar= allArticleTitles[0]; + + for( let i = 0; i < allArticleTitles.length; i++){ + const currentTitle = allArticleTitles[i]; + if(currentTitle.split(' ').length < fewestSoFar.split(' ').length){ + fewestSoFar = currentTitle; + } + } + return fewestSoFar; } /* @@ -23,7 +37,16 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + const articleWithNumbers = []; + for(let i = 0; i < allArticleTitles.length; i++){ + for(let j = 0; j < allArticleTitles[i].length; j++){ + if(Number(allArticleTitles[i][j])){ + articleWithNumbers.push(allArticleTitles[i]); + break; + } + } + } + return articleWithNumbers; } /* @@ -31,7 +54,11 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO + let totalNumbers = 0; + for (let i = 0; i < allArticleTitles.length; i++){ + totalNumbers += allArticleTitles[i].length; + } + return Math.round(totalNumbers/allArticleTitles.length); } diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..c8340f4a 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -33,10 +33,20 @@ 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 + const averagePrice = closingPricesForAllStocks.map(calculateAverage); + return averagePrice; } +function calculateAverage(companyStocks){ //callback function + let sum = 0; + for (let j = 0; j < companyStocks.length; j++) { + sum += companyStocks[j]; + } + const average = sum / companyStocks.length; + return Number(average.toFixed(2)); +} /* 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 @@ -46,11 +56,32 @@ function getAveragePrices(closingPricesForAllStocks) { In this example it would be: (Apple's price on the 5th day) - (Apple's price on the 1st day) = 172.99 - 179.19 = -6.2 The price change value should be rounded to 2 decimal places, and should be a number (not a string) -*/ +// */ +// function getPriceChanges(closingPricesForAllStocks) { +// const priceChanges = []; + +// for (let i = 0; i < closingPricesForAllStocks.length; i++) { + +// const prices = closingPricesForAllStocks[i]; +// const priceChange = prices[prices.length - 1] - prices[0]; +// priceChanges.push(Number(priceChange.toFixed(2))); +// } + +// return priceChanges; +// } + function getPriceChanges(closingPricesForAllStocks) { - // TODO + const priceChanges = closingPricesForAllStocks.map(calculatePriceChange); + return priceChanges; } - +function calculatePriceChange(companyStocks){ + for (let i = 0; i < companyStocks.length; i++) { + const priceChange = companyStocks[companyStocks.length - 1] - companyStocks[0]; + return Number(priceChange.toFixed(2)); + } +} + + /* 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 +95,25 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO + const descriptions = []; + + for (let i = 0; i < stocks.length; i++) { + const stockName = stocks[i].toUpperCase(); + let highestPrice = 0; + + for (let j = 0; j < 5; j++) { + const price = closingPricesForAllStocks[i][closingPricesForAllStocks[i].length - 1 - j]; + + if (price > highestPrice) { + highestPrice = price; + } + } + + const description = `The highest price of ${stockName} in the last 5 days was ${highestPrice}`; + descriptions.push(description); + } + + return descriptions; } @@ -88,7 +137,7 @@ test("should return a description of the highest price for each stock", () => { "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" + "The highest price of TSLA in the last 5 days was 1101.3" ] ); });