diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..a4e4aa3f --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "configurations": [ + { + "type": "node", + "name": "vscode-jest-tests.v2", + "request": "launch", + "args": [ + "test", + "--", + "--runInBand", + "--watchAll=false", + "--testNamePattern", + "${jest.testNamePattern}", + "--runTestsByPath", + "${jest.testFile}" + ], + "cwd": "c:\\Users\\pnasr\\OneDrive\\Documents\\GitHub\\RecipeCoursework\\JavaScript-Core-1-Coursework-Week3", + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "disableOptimisticBPs": true, + "runtimeExecutable": "npm" + } + ] +} \ No newline at end of file diff --git a/1-exercises/A-undefined/exercise.js b/1-exercises/A-undefined/exercise.js index 0acfc78d..e18b0766 100644 --- a/1-exercises/A-undefined/exercise.js +++ b/1-exercises/A-undefined/exercise.js @@ -10,11 +10,13 @@ */ // Example 1 +// 'a' is not assigned to anything, so it treats as an undefined data. let a; console.log(a); // Example 2 +// 'message' is not defined before it is used. function sayHello() { let message = "Hello"; } @@ -24,6 +26,7 @@ console.log(hello); // Example 3 +// There is no data in sayHelloToUser to place as user. function sayHelloToUser(user) { console.log(`Hello ${user}`); } @@ -32,5 +35,6 @@ sayHelloToUser(); // Example 4 +// The array arr has 3 indexes which are [0], [1] and [2]. So there is no index 3. let arr = [1,2,3]; console.log(arr[3]); diff --git a/1-exercises/B-array-literals/exercise.js b/1-exercises/B-array-literals/exercise.js index 51eba5cc..99cd8608 100644 --- a/1-exercises/B-array-literals/exercise.js +++ b/1-exercises/B-array-literals/exercise.js @@ -4,15 +4,14 @@ 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 --------------------------- */ console.log(numbers); console.log(mentors); - /* EXPECTED RESULT --------------- diff --git a/1-exercises/C-array-get-set/exercise.js b/1-exercises/C-array-get-set/exercise.js index 5ca911d5..2b09225b 100644 --- a/1-exercises/C-array-get-set/exercise.js +++ b/1-exercises/C-array-get-set/exercise.js @@ -5,11 +5,13 @@ */ function first(arr) { - return; // complete this statement + let f=arr[0]; + return(f); // complete this statement } function last(arr) { - return; // complete this statement + let l=arr[arr.length-1]; + return(l); // 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..1d339593 100644 --- a/1-exercises/C-array-get-set/exercises2.js +++ b/1-exercises/C-array-get-set/exercises2.js @@ -8,6 +8,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..24c6fd0f 100644 --- a/1-exercises/D-for-loop/exercise.js +++ b/1-exercises/D-for-loop/exercise.js @@ -27,7 +27,17 @@ const AGES = [ ]; // TODO - Write for loop code here - + function matchValues(WRITERS, AGES){ + for (let i = 0; i < WRITERS.length; i++){ + output = console.log(`${WRITERS[i]} is ${AGES[i]} years old`) + } + } + matchValues(WRITERS, AGES) +// for (let names of WRITERS){ +// for (let ages of AGES){ +// console.log(`${names} is ${ages} 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..c4d93a60 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(birthdays) { + let i = 0; + let birthdaysLength = birthdays.length; + while(i < birthdaysLength) { + 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..737b5da6 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,8 +12,11 @@ */ function getTemperatureReport(cities) { - // TODO -} + const temperatureReport = cities.map(city => { + return `The temperature in ${city} is ${temperatureService(city)} degrees` +}); + return temperatureReport + } /* ======= TESTS - DO NOT MODIFY ===== */ @@ -60,4 +63,4 @@ test("should return a temperature report for the user's cities (alternate input) test("should return an empty array if the user hasn't selected any cities", () => { expect(getTemperatureReport([])).toEqual([]); -}); \ No newline at end of file +}); diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..6699efaf 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 + let titles = []; + for (let articleTitle of allArticleTitles){ + if (articleTitle.length <= 65 ) { + titles.push(articleTitle); + } + } + return titles; } /* @@ -14,7 +20,9 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { - // TODO + + const fewestWord = (left, right) => left.length <= right.length ? left : right; + return allArticleTitles.reduce(fewestWord); } /* @@ -23,7 +31,13 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - // TODO + newList = []; + for (headline of allArticleTitles){ + if (/\d/.test(headline) == true){ + newList.push(headline); + } + } + return newList; } /* @@ -31,9 +45,21 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { - // TODO -} +// var total = 0; +// var count = 0; + +// allArticleTitles.forEach(averageNumberOfCharacters(item, index) { +// total += item; +// count += 1; +// }); +// return total / count; + + // works with numbers too +avg = Math.round(allArticleTitles.join('').length / allArticleTitles.length) + +return avg; + } /* ======= List of Articles - DO NOT MODIFY ===== */ @@ -50,6 +76,7 @@ const ARTICLE_TITLES = [ "Brussels urges Chile's incoming president to endorse EU trade deal", ]; +potentialHeadlines(ARTICLE_TITLES) /* ======= TESTS - DO NOT MODIFY ===== */ test("should only return potential headlines", () => { @@ -79,3 +106,4 @@ test("should only return headlines containing numbers", () => { test("should return the average number of characters in a headline", () => { expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65); }); + diff --git a/2-mandatory/3-stocks.js b/2-mandatory/3-stocks.js index 72d62f94..1a53858e 100644 --- a/2-mandatory/3-stocks.js +++ b/2-mandatory/3-stocks.js @@ -11,11 +11,11 @@ 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 - [340.69, 342.45, 334.69, 333.20, 327.29], // MSFT - [3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN - [2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL - [1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA + [179.19, 180.33, 176.28, 175.64, 172.99], // AAPL + [340.69, 342.45, 334.69, 333.2, 327.29], // MSFT + [3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN + [2951.88, 2958.13, 2938.33, 2928.3, 2869.45], // GOOGL + [1101.3, 1093.94, 1067.0, 1008.87, 938.53], // TSLA ]; /* @@ -34,7 +34,16 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ Functions can help with this! */ function getAveragePrices(closingPricesForAllStocks) { - // TODO + finalList = []; + for (let eachList of CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS) { + let total = 0; + for (let items of eachList) { + total += items; + } + let average = total.toFixed(2) / eachList.length; + finalList.push(Number(average.toFixed(2))); + } + return finalList; } /* @@ -48,7 +57,12 @@ 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 + finalList = []; + for (let item of CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS) { + priceChange = item[4] - item[0]; + finalList.push(Number(priceChange.toFixed(2))); + } + return finalList; } /* @@ -64,31 +78,73 @@ function getPriceChanges(closingPricesForAllStocks) { The price should be shown with exactly 2 decimal places. */ function highestPriceDescriptions(closingPricesForAllStocks, stocks) { - // TODO -} + let highestPrices = []; + let uppercaseList = []; + let finalList = []; + + for (let item of stocks) { + var uppercase = String(item).toUpperCase(); + uppercaseList.push(uppercase); + } + + for (let list of closingPricesForAllStocks) { + var max = list.reduce((a, b) => Math.max(a, b)); + highestPrices.push(max.toFixed(2)); + } + + let i = 0; + let arrayLength = uppercaseList.length; + do { + var txt = `The highest price of ${uppercaseList[i]} in the last 5 days was ${highestPrices[i]}`; + finalList.push(txt); + i++; + } while (i < arrayLength); + return finalList; +// I tried many different ways to use for loop or Map methods, but none of them worked! + // for (let item of uppercaseList) { + // for (let maxprice of highestPrices){ + // var x = `The highest price of ${item} in the last 5 days was ${maxprice}`; + // finalList.push(x); + // } + // return finalList; + // } + + // const pricemap = new Map(); + // pricemap.set(uppercaseList, highestPrices); + // for (item of stocks){ + // pricemap.forEach(function (value, key) { + + // for (const x of pricemap.keys()){ + // for (const y of pricemap.values()){ + + // finalList += `The highest price of ${x} in the last 5 days was ${y}`; + // }; + // }; + // return finalList; +} /* ======= TESTS - DO NOT MODIFY ===== */ test("should return the average price for each stock", () => { - expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( - [176.89, 335.66, 3405.66, 2929.22, 1041.93] - ); + expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([ + 176.89, 335.66, 3405.66, 2929.22, 1041.93, + ]); }); test("should return the price change for each stock", () => { - expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual( - [-6.2, -13.4, 23.9, -82.43, -162.77] - ); + expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([ + -6.2, -13.4, 23.9, -82.43, -162.77, + ]); }); test("should return a description of the highest price for each stock", () => { - expect(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)).toEqual( - [ - "The highest price of AAPL in the last 5 days was 180.33", - "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" - ] - ); + expect( + highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS) + ).toEqual([ + "The highest price of AAPL in the last 5 days was 180.33", + "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", + ]); });