-
-
Notifications
You must be signed in to change notification settings - Fork 283
London 10 | Shahid Amin | JavaScript-Core-1-Coursework-Week3 #277
base: main
Are you sure you want to change the base?
Changes from all commits
e2a76a7
8ead1a5
8987441
24b581f
5915395
be6cc79
70c00b1
316c436
f0e8f2b
616c53f
c65b895
8a908e8
d1e8f39
efc468d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. |
||
/* | ||
DO NOT EDIT BELOW THIS LINE | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
|
||
/* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
|
||
let numbers = [1, 2, 3]; // Don't change this array literal declaration | ||
|
||
numbers[3] = numbers.push(4) | ||
//The first value is 1??? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good spot I'll forward that observation. But if it wasn't a |
||
/* | ||
DO NOT EDIT BELOW THIS LINE | ||
--------------------------- */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,14 @@ const AGES = [ | |
49 | ||
]; | ||
|
||
AGES.forEach(k => { | ||
WRITERS.forEach(i => { | ||
console.log(i + " is " + k + " years old") | ||
}) | ||
}) | ||
Comment on lines
+29
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't look correct. Currently, it will say that every writer is 59,40,41,63 and 49 years old. |
||
|
||
|
||
|
||
// TODO - Write for loop code here | ||
|
||
/* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,12 @@ const BIRTHDAYS = [ | |
|
||
function findFirstJulyBDay(birthdays) { | ||
// TODO | ||
for (let dateOfBirth of BIRTHDAYS) { | ||
let getMonth = dateOfBirth.split(' ')[0]; | ||
if (getMonth === "July") { | ||
return dateOfBirth; | ||
} | ||
} | ||
Comment on lines
20
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job on this. |
||
} | ||
|
||
console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,12 @@ | |
*/ | ||
|
||
function getTemperatureReport(cities) { | ||
// TODO | ||
let report = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation doesn't look correct. |
||
for (let city of cities) { | ||
let temparatureOfCity = temperatureService(city); | ||
report.push(`The temperature in ${city} is ${temparatureOfCity} degrees`); | ||
} | ||
return report; | ||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,12 @@ | |
Implement the function below, which will return a new array containing only article titles which will fit. | ||
*/ | ||
function potentialHeadlines(allArticleTitles) { | ||
// TODO | ||
const shortArticleTitles = []; | ||
for (let articleTitle of allArticleTitles) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation seem off here too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job overall code inside the function will work correct but it doesn't seem to output a value. You are definitely missing something here. |
||
if (articleTitle.length <= 65) { | ||
shortArticleTitles.push(articleTitle); | ||
} | ||
} | ||
} | ||
|
||
/* | ||
|
@@ -14,7 +19,16 @@ function potentialHeadlines(allArticleTitles) { | |
(you can assume words will always be seperated by a space) | ||
*/ | ||
function titleWithFewestWords(allArticleTitles) { | ||
// TODO | ||
let fewestword = allArticleTitles[0].split(' ').length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well done. |
||
let result; | ||
for (let articleTitle of allArticleTitles) { | ||
let articleWords = articleTitle.split(' ').length; | ||
if (articleWords < fewestword ) { | ||
fewestword = articleWords; | ||
result = articleTitle; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
/* | ||
|
@@ -23,7 +37,17 @@ 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 regex = /[0-9]/; | ||
let result = []; | ||
for (let articleTitle of allArticleTitles) { | ||
for (let i = 0; i < articleTitle.length; i++) { | ||
if(articleTitle[i].match(regex)){ | ||
result.push(articleTitle); | ||
break; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
/* | ||
|
@@ -32,6 +56,21 @@ function headlinesWithNumbers(allArticleTitles) { | |
*/ | ||
function averageNumberOfCharacters(allArticleTitles) { | ||
// TODO | ||
|
||
let arrayOfLengths = allArticleTitles.map(w => w.length); | ||
console.log(arrayOfLengths) | ||
let sumOfLengths = 0; | ||
for (let i = 0; i < arrayOfLengths.length; i++) { | ||
sumOfLengths += arrayOfLengths[i]; | ||
console.log(sumOfLengths) | ||
} | ||
|
||
let sumOfArrayLengths = arrayOfLengths.length | ||
console.log(sumOfArrayLengths) | ||
|
||
let result = sumOfLengths/sumOfArrayLengths; | ||
console.log(result) | ||
Comment on lines
+60
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation is off here too. |
||
|
||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,46 @@ | |
*/ | ||
|
||
/* ======= Stock data - DO NOT MODIFY ===== */ | ||
|
||
/* Thinking throught the steps | ||
|
||
let keys = STOCKS | ||
|
||
let arrayA = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[0] | ||
let arrayM = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[1] | ||
let arrayAZ = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[2] | ||
let arrayG = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[3] | ||
let arrayT = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[4] | ||
|
||
|
||
let totalA = arrayA.reduce(function(a,b){ return +a + +b; }); | ||
console.log(totalA) | ||
|
||
let totalM = arrayM.reduce(function(a,b){ return +a + +b; }); | ||
console.log(totalM) | ||
|
||
let totalAZ = arrayAZ.reduce(function(a,b){ return +a + +b; }); | ||
console.log(totalAZ) | ||
|
||
let totalG = arrayG.reduce(function(a,b){ return +a + +b; }); | ||
console.log(totalG) | ||
|
||
let totalT = arrayT.reduce(function(a,b){ return +a + +b; }); | ||
console.log(totalT) | ||
|
||
|
||
totalA = Math.round(totalA).toFixed(2) | ||
totalM = Math.round(totalM).toFixed(2) | ||
totalAZ = Math.round(totalT).toFixed(2) | ||
totalG = Math.round(totalG).toFixed(2) | ||
totalT = Math.round(totalT).toFixed(2) | ||
|
||
|
||
|
||
let finalArray = [totalA/5, totalM/5, totalAZ/5, totalG/5, totalT/5] | ||
|
||
console.log(finalArray)*/ | ||
|
||
const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"]; | ||
|
||
const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | ||
|
@@ -18,6 +58,8 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
[1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA | ||
]; | ||
|
||
console.log(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS [0]) | ||
|
||
/* | ||
We want to understand what the average price over the last 5 days for each stock is. | ||
Implement the below function, which | ||
|
@@ -35,6 +77,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [ | |
*/ | ||
function getAveragePrices(closingPricesForAllStocks) { | ||
// TODO | ||
let averageprice=[]; | ||
|
||
for(const stocksline of closingPricesForAllStocks){ | ||
let sum = 0; | ||
let average = 0; | ||
for(const item of stocksline){ | ||
sum = sum + item; | ||
|
||
} | ||
average = sum / stocksline.length; | ||
averageprice.push(Math.round(average * 100) / 100); | ||
|
||
} | ||
return averageprice; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good effort. Please make sure you indent your code correctly. |
||
|
||
} | ||
|
||
/* | ||
|
@@ -49,6 +106,13 @@ function getAveragePrices(closingPricesForAllStocks) { | |
*/ | ||
function getPriceChanges(closingPricesForAllStocks) { | ||
// TODO | ||
const priceChange = []; | ||
for(const stocksline of closingPricesForAllStocks){ | ||
let sub = 0; | ||
sub = stocksline.slice(-1) - stocksline[0]; | ||
priceChange.push(Math.round(sub*100)/100); | ||
} | ||
return priceChange; | ||
} | ||
|
||
/* | ||
|
@@ -65,6 +129,27 @@ function getPriceChanges(closingPricesForAllStocks) { | |
*/ | ||
function highestPriceDescriptions(closingPricesForAllStocks, stocks) { | ||
// TODO | ||
const result = []; | ||
let num = 0; | ||
let Name; | ||
for(const stocksline of closingPricesForAllStocks){ | ||
|
||
|
||
let FirstPrice = stocksline[0]; | ||
let i = 1; | ||
while(i<stocksline.length){ | ||
let price = stocksline[i]; | ||
if(price>FirstPrice){ | ||
FirstPrice = price; | ||
} | ||
i=i+1; | ||
} | ||
|
||
Name = stocks[num].toUpperCase(); | ||
result.push(`The highest price of ${Name} in the last 5 days was ${FirstPrice.toFixed(2)}`); | ||
num = num + 1; | ||
} | ||
return result; | ||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good, but not every function will have parameters. There's something missing inside the function
sayHello
for it not to be undefined. Can you spot what it is?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A little hint