Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Open
10 changes: 9 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
*/

function getTemperatureReport(cities) {
// TODO
const citiesReport = [];

for (const city of cities){
let cityTemparature = temperatureService(city);
let statement = `The temperature in ${city} is ${cityTemparature} degrees`;

citiesReport.push(statement);
}
return citiesReport;
}


Expand Down
48 changes: 43 additions & 5 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO

let fittingArticles = [];

for (const title of allArticleTitles) {
if (title.length <= 65){
fittingArticles.push(title);
}
}
return fittingArticles;
}

/*
Expand All @@ -14,7 +22,13 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let shortestTitle = allArticleTitles[0];
for (const newTitle of allArticleTitles){
if (newTitle.length <= shortestTitle.length){
shortestTitle = newTitle;
}
}
return shortestTitle;
}

/*
Expand All @@ -23,17 +37,41 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let clickables = [];

for (const title of allArticleTitles){
for (const character of title){
if ('0123456789'.includes(character)){ //if (character = Number) my previous -failed- solution.
clickables.push(title);
break;
}

}
}
return clickables;
}

/*
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
}
let charactersCount= 0;
Comment thread
hussein-alsayed marked this conversation as resolved.
let titlesCount = 0;

for (const title of allArticleTitles){

if (title.length > 0){
charactersCount += title.length;

}

titlesCount++;
}

let average = charactersCount / titlesCount;
return Math.round(average);
}


/* ======= List of Articles - DO NOT MODIFY ===== */
Expand Down
51 changes: 48 additions & 3 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,22 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averagePrices = [];

for (const stock of CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is off in multiple places in this loop. Keeping them properly aligned makes it easier to see the flow of the code. Your IDE can help you with that, most also contain a hotkey to auto-indent and fix other styling issues

let pricesTotal = 0;

for (const prices of stock){

pricesTotal += prices;
}

let averagePrice = Number((pricesTotal / stock.length).toFixed(2));
averagePrices.push(averagePrice);
}


return averagePrices;
}

/*
Expand All @@ -48,7 +63,13 @@ 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
const priceChanges = [];

for (const stock of CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS){
let priceChange = Number((stock[4] - stock[0]).toFixed(2));
priceChanges.push(priceChange);
}
return priceChanges;
}

/*
Expand All @@ -63,8 +84,32 @@ function getPriceChanges(closingPricesForAllStocks) {
The stock ticker should be capitalised.
The price should be shown with exactly 2 decimal places.
*/

function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
const highestPriceReports = [];
let pricesArray = 0;


for (const stock of stocks){
let highestPrice = 0;
let stockPrices = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[pricesArray];

for (const price of stockPrices){

if (price > highestPrice){
highestPrice = price;
}

}

// highestPrice = Number(highestPrice.toFixed(2));
//(Number object switchh the value from "string" to numbers, which don't see the onely zero at the decimal place as a number then).
highestPriceReports.push(`The highest price of ${stock.toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`);
pricesArray++;

}

return highestPriceReports;
}


Expand Down