javascript - What goes wrong if 77>602? -
what goes wrong if 77>602? tried in ie, firefox , chrome
function getmaxvalue(data){ var maxvalue=0; for(var i=0;i<data.length;i++){ if(data[i].value>maxvalue){ console.log(data[i].value +">"+maxvalue); maxvalue=data[i].value; } } console.log("maxvalue:"+maxvalue); return maxvalue; }
i data json:
[{ "keyword": "user: allen-p", "value": "602" }, { "keyword": "from: phillip.allen@enron.com", "value": "598" }, { "keyword": "date: 2001", "value": "276" }, { "keyword": "subject: re:", "value": "228" }, { "keyword": "date: 2001 apr", "value": "77" }, ]
needed add useless description stackoverflow. please me;). json file bit bigger , example.
strings compared alphabetically if contain numbers. character '7'
comes after character '6'
, alphabetically, indeed, in terms of strings, "77" > "602"
.
the solution convert them numbers first:
if(parsefloat(data[i].value) > maxvalue){
or sake of brevity, unary +
operator this:
if(+data[i].value > maxvalue){
Comments
Post a Comment