From 2d01c4eca3948867a55dc810c9a4c0009c6bfe6f Mon Sep 17 00:00:00 2001 From: nfarah22 <61600465+nfarah22@users.noreply.github.com> Date: Tue, 18 Apr 2023 15:41:05 +0100 Subject: [PATCH] done done --- 1-exercises/A-undefined/exercise.js | 8 +- 1-exercises/B-array-literals/exercise.js | 4 +- 1-exercises/C-array-get-set/exercise.js | 4 +- 1-exercises/C-array-get-set/exercises2.js | 2 +- 1-exercises/D-for-loop/exercise.js | 11 ++ .../E-while-loop-with-array/exercise.js | 13 ++- 2-mandatory/1-weather-report.js | 25 ++++- 2-mandatory/2-financial-times.js | 100 ++++++++++++++---- 2-mandatory/3-stocks.js | 53 +++++++++- 9 files changed, 184 insertions(+), 36 deletions(-) diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..d4a3f197 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -12,16 +12,18 @@ // Example 1 let a; console.log(a); - +// element a has no value // Example 2 function sayHello() { let message = "Hello"; } + let hello = sayHello(); console.log(hello); +// the function is missing from the callback // Example 3 function sayHelloToUser(user) { @@ -30,7 +32,9 @@ function sayHelloToUser(user) { sayHelloToUser(); - +// the function is missing the callback variable // Example 4 let arr = [1,2,3]; console.log(arr[3]); + +// index 3 does not exist in the array \ No newline at end of file diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..211593f3 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","Douglas","Irina","Michael"] ;// 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..0ea3036d 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.pop(); // 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..84656214 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.js @@ -27,7 +27,18 @@ const AGES = [ ]; // TODO - Write for loop code here +function intro(){ +let list = [] +for (let i = 0;i < WRITERS.length;){ + list.push(`${WRITERS[i]}is ${AGES[i]} years old`) + i++ +} +return list + +} + +console.log(intro()) /* 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..6e9df98c 100644 --- a/1-exercises/E-while-loop-with-array/exercise.js +++ b/1-exercises/E-while-loop-with-array/exercise.js @@ -16,8 +16,15 @@ const BIRTHDAYS = [ "November 15th" ]; -function findFirstJulyBDay(birthdays) { - // TODO +function findFirstJulyBDay() { + let first = ' '; + let arr = 0 + while(arr < BIRTHDAYS.length){ if (BIRTHDAYS[arr].includes('July 11th')){ + first=BIRTHDAYS[arr]; + } + arr ++ + } + return first } -console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" +console.log(findFirstJulyBDay()); // should output "July 11th" diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..e56b1fb5 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -10,10 +10,29 @@ For example, "The temperature in London is 10 degrees" - Hint: you can call the temperatureService function from your function */ - +let usersCities = [ + "London", + "Paris", + "São Paulo", + "Barcelona", + "Dubai" +] function getTemperatureReport(cities) { - // TODO -} + let report = []; + + for (let i = 0; i < cities.length; i++) { + let city = cities[i]; + let temperature = temperatureService(city); + + report.push(`The temperature in ${city} is ${temperature} degrees`); + } + + return report; + } + + + + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..3fa00fe4 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -4,8 +4,38 @@ 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 + +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", +]; + + + + + + + + +function potentialHeadlines(ARTICLE_TITLES) { +let newArray= []; + + for (let i=0;i < ARTICLE_TITLES.length; i++){ + if ( ARTICLE_TITLES[i].length<= 65){ + newArray.push(ARTICLE_TITLES[i]) + } + + } + + return newArray } /* @@ -13,17 +43,49 @@ function potentialHeadlines(allArticleTitles) { 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 +function titleWithFewestWords(ARTICLE_TITLES) { + let smallestTitle= ARTICLE_TITLES[0]; + + for (let i = 1; i < ARTICLE_TITLES.length; i++) { + if (ARTICLE_TITLES[i].split(' ').length < smallestTitle.split(' ').length) { + smallestTitle = ARTICLE_TITLES[i]; + } + } + + return smallestTitle; } + /* 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 + function headlinesWithNumbers(allArticleTitles) { + let headlines = []; + + for (let i = 0; i < allArticleTitles.length; i++) { + let title = allArticleTitles[i]; + let hasNumber = false; + + for (let j = 0; j < title.length; j++) { + let char = title[j]; + + if (char >= '0' && char <= '9') { + hasNumber = true; + break; + } + } + + if (hasNumber) { + headlines.push(title); + } + } + + return headlines; + } + } /* @@ -31,24 +93,24 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO -} + let totalCharacters = 0; + + for (let i = 0; i < allArticleTitles.length; i++) { + let title = allArticleTitles[i]; + totalCharacters += title.length; + } + + let averageCharacters = totalCharacters / allArticleTitles.length; + let roundedAverageCharacters = Math.round(averageCharacters); + + return roundedAverageCharacters; + } + /* ======= 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", -]; + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..584e4a9c 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -34,7 +34,26 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + function getAveragePrices(closingPricesForAllStocks) { + let averages = []; + + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let prices = closingPricesForAllStocks[i]; + let total = 0; + + for (let j = 0; j < prices.length; j++) { + total += prices[j]; + } + + let average = total / prices.length; + let roundedAverage = Number(average.toFixed(2)); + + averages.push(roundedAverage); + } + + return averages; + } + } /* @@ -48,7 +67,22 @@ 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 + function getPriceChanges(closingPricesForAllStocks) { + let changes = []; + + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let prices = closingPricesForAllStocks[i]; + let firstPrice = prices[0]; + let lastPrice = prices[prices.length - 1]; + let change = lastPrice - firstPrice; + let roundedChange = Number(change.toFixed(2)); + + changes.push(roundedChange); + } + + return changes; + } + } /* @@ -64,8 +98,19 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO -} + let descriptions = []; + + for (let i = 0; i < closingPricesForAllStocks.length; i++) { + let prices = closingPricesForAllStocks[i]; + let highestPrice = Math.max(...prices); + let stock = stocks[i].toUpperCase(); + let description = `The highest price of ${stock} in the last 5 days was ${highestPrice.toFixed(2)}`; + descriptions.push(description); + } + + return descriptions; + } + /* ======= TESTS - DO NOT MODIFY ===== */