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