London 9 - George Primentas - JavaScript Core 1 - Week 3 - #191
London 9 - George Primentas - JavaScript Core 1 - Week 3#191GeorgePrimentas wants to merge 2 commits into
Conversation
| function highestPrice(closingPrices) { | ||
| highestPrices = []; | ||
| for (let price of closingPrices) { | ||
| highestPrices.push(/*Number*/((Math.max.apply(null, price)).toFixed(2))) // Found this solution here: https://stackoverflow.com/questions/1669190/find-the-min-max-element-of-an-array-in-javascript |
There was a problem hiding this comment.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max Please write about finding max number in array. I found the easiest way for me to find max number in array using spread syntax.
const arr = [1, 2, 3];
const max = Math.max(...arr);
| highestPrices = []; | ||
| for (let price of closingPrices) { | ||
| highestPrices.push(/*Number*/((Math.max.apply(null, price)).toFixed(2))) // Found this solution here: https://stackoverflow.com/questions/1669190/find-the-min-max-element-of-an-array-in-javascript | ||
| // It works but I don't understand it - If I kept the Number method it would show the last higestPrice with one decimal after the point; not two... |
There was a problem hiding this comment.
When you have Number and you're using toFixed(2) method on Number. You will have 2 decimal number after dot only if they are not including zero at the end.
Examples of numbers:
10.563 => 10.56
10.100 => 10.1
When you have String - you will have two decimalsafter dot in any case. But the number will be a String.
"10.563" => "10.56"
"10.100" => "10.10"
So that's why it is better to leave the highest price as a String. Because in test we have a price "1101.30", not "1101.3". And we should only to show the price in sentence, we don't have any test which is checking if the price is a number.
VitalinaKuzmenko
left a comment
There was a problem hiding this comment.
Hi! I love you solutions! I left some comments for you! Let me know if you need anything!
Exercises & Mandatory exercises completed