Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
4 changes: 4 additions & 0 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand All @@ -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}`);
}
Expand All @@ -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]);
5 changes: 2 additions & 3 deletions 1-exercises/B-array-literals/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------
Expand Down
6 changes: 4 additions & 2 deletions 1-exercises/C-array-get-set/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/*
Expand Down
1 change: 1 addition & 0 deletions 1-exercises/C-array-get-set/exercises2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------------- */
Expand Down
12 changes: 11 additions & 1 deletion 1-exercises/D-for-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
13 changes: 10 additions & 3 deletions 1-exercises/E-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 6 additions & 3 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===== */
Expand Down Expand Up @@ -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([]);
});
});
38 changes: 33 additions & 5 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*
Expand All @@ -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);
}

/*
Expand All @@ -23,17 +31,35 @@ 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;
}

/*
The Financial Times wants to understand what the average number of characters in an article title is.
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 ===== */
Expand All @@ -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", () => {
Expand Down Expand Up @@ -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);
});

104 changes: 80 additions & 24 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
];

/*
Expand All @@ -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;
}

/*
Expand All @@ -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;
}

/*
Expand All @@ -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",
]);
});