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
88 changes: 53 additions & 35 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,72 @@
For example, "The temperature in London is 10 degrees"
- Hint: you can call the temperatureService function from your function
*/
const cities = [
"London",
"Paris",
"Barselona",
"Dubai",
"Mumbai",
"São Paulo",
"Logos",
];

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

for (const item of cities) {
citystrings.push(

Choose a reason for hiding this comment

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

Will be nice to see there String Interpolation👌

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with that 😄 You can read more about template literals in JavaScript here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

"The temperature in" +
" " +
item +
" " +
"is" +
" " +
temperatureService(item) +
" " +
"degrees"
);
}
return citystrings;
}


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

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

temparatureMap.set('London', 10);
temparatureMap.set('Paris', 12);
temparatureMap.set('Barcelona', 17);
temparatureMap.set('Dubai', 27);
temparatureMap.set('Mumbai', 29);
temparatureMap.set('São Paulo', 23);
temparatureMap.set('Lagos', 33);
return temparatureMap.get(city);
let temparatureMap = new Map();

temparatureMap.set("London", 10);
temparatureMap.set("Paris", 12);
temparatureMap.set("Barcelona", 17);
temparatureMap.set("Dubai", 27);
temparatureMap.set("Mumbai", 29);
temparatureMap.set("São Paulo", 23);
temparatureMap.set("Lagos", 33);

return temparatureMap.get(city);
}

test("should return a temperature report for the user's cities", () => {
let usersCities = [
"London",
"Paris",
"São Paulo"
]

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in London is 10 degrees",
"The temperature in Paris is 12 degrees",
"The temperature in São Paulo is 23 degrees"
]);
let usersCities = ["London", "Paris", "São Paulo"];

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in London is 10 degrees",
"The temperature in Paris is 12 degrees",
"The temperature in São Paulo is 23 degrees",
]);
});

test("should return a temperature report for the user's cities (alternate input)", () => {
let usersCities = [
"Barcelona",
"Dubai"
]

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in Barcelona is 17 degrees",
"The temperature in Dubai is 27 degrees"
]);
let usersCities = ["Barcelona", "Dubai"];

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in Barcelona is 17 degrees",
"The temperature in Dubai is 27 degrees",
]);
});

test("should return an empty array if the user hasn't selected any cities", () => {
expect(getTemperatureReport([])).toEqual([]);
});
expect(getTemperatureReport([])).toEqual([]);
});

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

Choose a reason for hiding this comment

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

what is the purpose of the const 'newarray'?


for (const item of allArticleTitles) {

Choose a reason for hiding this comment

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

Nice !

if (item.length <= 65) {
newarray.push(item);
}
}
return newarray;
}


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

let fewestWord=ARTICLE_TITLES[0].split(' ').length;
let finalTitle=ARTICLE_TITLES[0];
let i=1;

while(i<ARTICLE_TITLES.length){

Choose a reason for hiding this comment

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

In this while loop, you are using 'i' as an iterator to go on loop until it reaches the condition i < ARTICLE_TITLES.length.

Is there another way to improve this loop so it's more clear what it's doing?

Copy link
Author

Choose a reason for hiding this comment

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

while(i=1, i<ARTICLE_TITLES.length, i++)

let result=ARTICLE_TITLES[i].split(' ').length;
let title=ARTICLE_TITLES[i];

if(result<fewestWord){
fewestWord=result;
finalTitle=title;
}

i=i+1;

}
return finalTitle;

}

/*
Expand All @@ -23,15 +50,45 @@ 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 newarray=[];
const number=/[0-9]/;

Choose a reason for hiding this comment

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

I see array like this for the first time, can you explain what it does ? I'm just curious 🙂

Copy link
Author

Choose a reason for hiding this comment

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

it means that it is(number) integer and it is not a letter or sth else

let i=0;
while (i < allArticleTitles.length) {

Choose a reason for hiding this comment

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

Same as the previous comment from the while loop

if (allArticleTitles[i].match(number)) {
newarray.push(allArticleTitles[i]);
}
i=i+1;
}
return newarray;
}


Choose a reason for hiding this comment

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

Just as a nice to have: try to avoid committing unnecessary empty lines like this so we can keep the Pull request cleaner and avoid extra changes

Copy link
Author

Choose a reason for hiding this comment

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

you are right, thanks for your advice
















/*
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 sum=0;
for (const item of allArticleTitles) {
sum = (item.length)+sum;

Choose a reason for hiding this comment

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

why is item.length to be inside the () ?
is there another way to do a 'sum = variable + sum'? a shorter way?

Copy link
Author

Choose a reason for hiding this comment

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

sum += item.length


}
return Math.round(sum / allArticleTitles.length);
}


Expand Down
54 changes: 51 additions & 3 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
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;

}

/*
Expand All @@ -48,7 +62,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 priceChange = [];
for(const stocksline of closingPricesForAllStocks){
let sub = 0;
sub = stocksline.slice(-1) - stocksline[0];
priceChange.push(Math.round(sub*100)/100);
}
return priceChange;
}

/*
Expand All @@ -64,10 +84,32 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
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){

Choose a reason for hiding this comment

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

Same comment for the while loop as before

Copy link
Author

Choose a reason for hiding this comment

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

while(i=1, i<stocksline.length, i++){ }

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;
}




/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
Expand All @@ -92,3 +134,9 @@ test("should return a description of the highest price for each stock", () => {
]
);
});