Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

Glasgow_6-Ehdaa Sakawi-JS_Core_1_Week_3 #268

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
12 changes: 10 additions & 2 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@
*/

function getTemperatureReport(cities) {
// TODO
let report = [];
for (let city of cities) {
let temparature = temperatureService(city);
report.push( `The temperature in ${city} is ${temparature} degrees`);



}
return report;
}


/* ======= TESTS - DO NOT MODIFY ===== */

Expand Down
41 changes: 39 additions & 2 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
// TODO l
let headlines=[];



for(let title of allArticleTitles) {
if(title.length <= 65) {
headlines.push(title);
}
}


return headlines;
}

/*
Expand All @@ -14,6 +26,18 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
let result = allArticleTitles[0];
let resultlengh = result.split(' ').length;
for ( let i=1; i< allArticleTitles.length; i++ ){

let words = allArticleTitles[i].split(' ');
if (resultlengh > words.length){
result= allArticleTitles[i]
resultlengh = words.length

}
}
return result;
// TODO
}

Expand All @@ -23,6 +47,14 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {

let headlines = [];
for (let title of allArticleTitles){
if (/\d/.test(title)){
headlines.push(title);
}
}
return headlines;
// TODO
}

Expand All @@ -31,8 +63,13 @@ function headlinesWithNumbers(allArticleTitles) {
Implement the function below to return this number - rounded to the nearest integer.
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
totalNumbers = 0;
for (let i = 0; i < allArticleTitles.length; i++){
totalNumbers += allArticleTitles[i].length;
}
return Math.round(totalNumbers/allArticleTitles.length);
}




Expand Down
65 changes: 61 additions & 4 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,36 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO

let averagePrices = [];
for (let stockPrices of closingPricesForAllStocks){
let sumOfPrices = 0;
for (let price of stockPrices) {
sumOfPrices = sumOfPrices + price;
}
let averagePrice = sumOfPrices / stockPrices.length;
let roundedAverage = Math.round(averagePrice * 100) / 100;
averagePrices.push(roundedAverage);
}



return averagePrices;
}
// TODO

/* let averagePrice = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++){
let sum = 0;
for (let j = 0; j < closingPricesForAllStocks[i].lenght; j++){
sum += closingPricesForAllStocks[i][j];
}
averagePrice.push(Number((sum / closingPricesForAllStocks[i].lenght).toFixed(2)));

}
return averagePrice; */



/*
We also want to see what the change in price is from the first day to the last day for each stock.
Expand All @@ -48,7 +76,16 @@ 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
let priceChanges = [];

for( let stockPrices of closingPricesForAllStocks){
let startPrice = stockPrices[0] * 100;
let closePrice = stockPrices[stockPrices.length - 1] * 100;
priceChanges.push(math.round(closePrice - startPrice) / 100);
}


return priceChanges;
}

/*
Expand All @@ -64,12 +101,32 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let highestPriceStrings = [];


for(let i = 0; i< stocks.length; i++){
let stockName = stocks[i].toUpperCase();
let stockPrices = closingPricesForAllStocks[i];

let highestPrice;
for (let price of stockPrices){
if (highestPrice == undefined || price > highestPrice){
highestPrice = price;
}
}
highestPriceStrings.push(`The highest price of ${"stockName"} in the last 5 days was ${highestPrice.toFixed(2)}`);
}





return highestPriceStrings;
}


/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
test.only("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]
);
Expand Down