Say I have a JS array like so…
var newArray = [20,182,757,85,433,209,57,828,635];
And I want to use this data to create a bar graph, where the height of the highest bar would == 100.
So, the 828 value bar needs to be set to 100, and the rest of the bars need to be calculated relative to that, and also to the closest integer. I am not sure how to go about this?
Is there a way to create a new array from the one above using a loop? Then I can use the new array?
First, calculate the max value:
Then, divide each element by that value, do it times 100, and round it down so that the highest value becomes 100:
.mapis essentially a loop, but the loop is done internally so you can write cleaner code. Do note it’s ES5, so you need a shim for older browsers.