score:9

As expected, d3.max() is indeed returning the maximum value... however, it is returning the maximum value among strings, not among numbers.

The explanation is that, by default, d3.csv() will load all the values as strings, even if they are numbers. Therefore, when you do:

var value = record[year];

value is a string, not a number. And in JavaScript the maximum for an array of strings is different from the maximum for an array of numbers. For instance, using numbers:

var myArray = [1, 65, 9, 32, 42];
console.log(d3.max(myArray));
<script src="https://d3js.org/d3.v4.min.js"></script>

That's the expected value. But look what happens if we use strings:

var myArray = ["1", "65", "9", "32", "42"];
console.log(d3.max(myArray));
<script src="https://d3js.org/d3.v4.min.js"></script>

Solution: change your values to numbers:

var value = +record[year];

Related Query

More Query from same tag