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
11 changes: 8 additions & 3 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
*/

function getTemperatureReport(cities) {
// TODO
const statements = [];
cities.forEach(city => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I believe this exercise was meant to be solved by simple iteration as forEach is only introduced during week 4. On the other hand if you are already using items from week 4 then prefer solving this exercise using map: https://syllabus.codeyourfuture.io/js-core-1/week-4/lesson#map

statements.push('The temperature in '.concat(city, ' is ',

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer using string interpolation instead of the concat method https://syllabus.codeyourfuture.io/js-core-1/week-1/lesson#string-concatenation

temperatureService(city), ' degrees'));
});
return statements;
}


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

function temperatureService(city) {
let temparatureMap = new Map();
let temparatureMap = new Map();

temparatureMap.set('London', 10);
temparatureMap.set('Paris', 12);
Expand All @@ -28,7 +33,7 @@ function temperatureService(city) {
temparatureMap.set('Mumbai', 29);
temparatureMap.set('São Paulo', 23);
temparatureMap.set('Lagos', 33);

return temparatureMap.get(city);
}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if it's a one liner feel free to immediately return the value without assigning it to a variable

return article.length < 65;
})
return smallArticle;
}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a good solution with simple use of an iterated array. You can of course also do it using forEach as well. If you are happy to investigate other solutions using inline functions (like you did with filter above), you can have a read of how the reduce function works that can be used to implement things like miniimums, maximums or sums: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

}

/*
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
}


function headlinesWithNumbers(allArticleTitles) {
let articleWithNums = [];
for (let i = 0; i < allArticleTitles.length; i++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Similarly you already used forEach for other solutions, feel free to do that here as well

if (/\d/.test(allArticleTitles[i])) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice use of regular expressions

articleWithNums.push(allArticleTitles[i]);
}
}
return articleWithNums;
}


/*
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 sumOfAllChars = 0;
allArticleTitles.forEach(article => {
sumOfAllChars += article.length;
});
return Math.round(sumOfAllChars / allArticleTitles.length);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is yet again a good solution. If you are interested the reduce function can also be used here to calculate the result

}


Expand Down Expand Up @@ -78,4 +100,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);
});
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's customary to have a newline at every file ending when handling code. Usually your IDE can be set up in a way to enforce this and most do it by default

37 changes: 34 additions & 3 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,28 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Solve the smaller problems, and then build those solutions back up to solve the larger problem.
Functions can help with this!
*/
// function getAveragePrices(closingPricesForAllStocks) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's usually good practive to remove commented code from PRs

// const averagePrices = closingPricesForAllStocks.map(stock => {
// const sumOfStocks = stock.reduce((accumulator, currentValue) => {

// return accumulator + currentValue;
// })
// return Math.round((sumOfStocks / stock.length) * 100) / 100;
// })
// return averagePrices;
// }

function getAveragePrices(closingPricesForAllStocks) {
// TODO
const averagePrices = [];
for (const closingPrices of closingPricesForAllStocks) {
let sum = 0;

for (const closingPrice of closingPrices) {
sum += closingPrice;
}
averagePrices.push(Math.round(sum / closingPrices.length * 100) / 100);
}
return averagePrices;
}

/*
Expand All @@ -48,7 +68,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
const priceChange = closingPricesForAllStocks.map(stock => {
const change = (stock[stock.length - 1] - stock[0]);
return Math.round(change * 100) / 100;

})
return priceChange;
}

/*
Expand All @@ -64,7 +89,13 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
const descriptions = [];
for (let index = 0; index < closingPricesForAllStocks.length; index++) {
const closingPrices = closingPricesForAllStocks[index];
const maxPrice = Math.max(...closingPrices).toFixed(2);
descriptions.push(`The highest price of ${stocks[index].toUpperCase()} in the last 5 days was ${maxPrice}`);
}
return descriptions;
}


Expand Down