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

London10-Onur-Atas-JavaScript-Core-1-Coursework-Week3 #283

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
8 changes: 7 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
*/

function getTemperatureReport(cities) {
// TODO
var temparatureInCities = [];

for (let i = 0; i < cities.length; i++) {
let temparature = temperatureService(cities[i])
temparatureInCities.push(`The temperature in ${cities[i]} is ${temparature} degrees`)
}
return temparatureInCities;
}


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

for (let i = 0; i < allArticleTitles.length; i++) {
const eachTitle = allArticleTitles[i];
if (eachTitle.length <= 65) {
articleTitles.push(eachTitle);
}
}

return articleTitles;
}




/*
The editor of the FT likes short headlines with only a few words!
Implement the function below, which returns the title with the fewest words.
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
}
function titleWithFewestWords(allArticleTitles){
var articleTitles = []

var lowest = Infinity;
var articleTitleWords;

for (var i = 0; i < allArticleTitles.length; i++) {
var words = allArticleTitles[i].split(" ");
if (words.length < lowest) {
lowest = words.length;
articleTitleWords = allArticleTitles[i];
}
}

return articleTitleWords;
}

/*
The editor of the FT has realised that headlines which have numbers in them get more clicks!
Implement the function below to return a new array containing all the headlines which contain a number.
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
const regex = /\d+/;
return allArticleTitles.filter(title => regex.test(title));
}


/*
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
const articleLength = allArticleTitles.reduce((acc, articleTitle) => acc + articleTitle.length, 0);
const averageLength = articleLength / allArticleTitles.length;
return Math.round(averageLength);
}




/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
"Streaming wars drive media groups to spend more than $100bn on new content",
Expand Down
47 changes: 41 additions & 6 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,23 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
}

const averageStocks = closingPricesForAllStocks.length;
const lastDays = closingPricesForAllStocks[0].length;
const averages = [];

for (let i = 0; i < averageStocks; i++) {
let sum = 0;
for (let j = 0; j < lastDays; j++) {
sum += closingPricesForAllStocks[i][j];
}
const average = sum / lastDays;
averages.push(parseFloat(average.toFixed(2)));
}

return averages;
}


/*
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,9 +63,19 @@ 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 averageStocks = closingPricesForAllStocks.length;
const changesInPrice = [];

for (let i = 0; i < averageStocks; i++) {
const firstDayPrice = closingPricesForAllStocks[i][0];
const lastDayPrice = closingPricesForAllStocks[i][closingPricesForAllStocks[i].length - 1];
const priceChange = parseFloat((lastDayPrice - firstDayPrice).toFixed(2));
changesInPrice.push(priceChange);
}

return changesInPrice;
}
/*
As part of a financial report, we want to see what the highest price was for each stock in the last 5 days.
Implement the below function, which
Expand All @@ -64,8 +89,18 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
}
const highestPrices = [];

for (let i = 0; i < stocks.length; i++) {
const stockPrices = closingPricesForAllStocks[i];
const highestPrice = Math.max(...stockPrices).toFixed(2);
const stockName = stocks[i].toUpperCase();
highestPrices.push(`The highest price of ${stockName} in the last 5 days was ${highestPrice}`);
}

return highestPrices;
}



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