Javascript comparing numbers isn't working

user6645395

I am trying to normalize data and realized that it's not working because I am getting the incorrect maximum. However, the comparing of numbers isn't working correctly. Here is my code:

var max = Number.MIN_VALUE;
var min = Number.MAX_VALUE;
for (i = 0; i < array.length; i++) 
{ 
    if(array[i]>max)
    {
        max = array[i];
    }
}

for (i = 0; i < array.length; i++) 
{ 
    if(array[i]<min)
    {
        min = array[i];
    }
}

console.log("max: " + max);
console.log("min: " + min);

for (i = 0; i < array.length; i++)
{
    if(array[i]!=0)
    {   
            if(array[i]>max)
            {
               console.log(i+": " + array[i] + " yes");
            }
            else
            {
               console.log(i+": " + array[i] + " no " + max);
            }

    }
}

I am getting a lot of console output, but here is one example:

241590: 17.5799 no 9.86874

meaning that for some reason, JS thinks that 17.5799 is not bigger than 9.86874. Why is this happening and what can I do to fix it? Thank you!

EDIT 1: When printing array[i] - max, I end up with the correct difference...except it's negative.

Darkves

It's because you'r array contains strings, not numbers.

so '9.95799' > '89.86874' => true :) Use numbers instead

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

javascript isn't working on html

From Dev

Javascript innerWidth isn't working

From Dev

For loop in Javascript isn't working

From Dev

Javascript innerWidth isn't working

From Dev

Javascript in PHP isn't working

From Dev

My JavaScript isn't working

From Dev

Javascript: For loop isn't working

From Dev

Key Up isn't working Javascript

From Dev

Why isn't this JavaScript mousemove function working?

From Dev

Javascript portion of my webpage isn't working

From Dev

Why isn't my JavaScript variable working?

From Dev

JavaScript toUpperCase isn't working. Why?

From Dev

Exactly the same javascript but one isn't working?

From Dev

Tableau Javascript API HTTPS isn't working

From Dev

The following simple Javascript code isn't working

From Dev

svg size isn't working with javascript

From Dev

Javascript Code isn't working for Date viewing?

From Dev

JavaScript with php variabe isn't working

From Dev

Basic javascript isn't working - display block

From Dev

Javascript Multiple Function isn't working

From Dev

why isn't this javascript validation working?

From Dev

Onchange isn't working but the javascript script is fine

From Dev

Why isn't my JavaScript variable working?

From Dev

Why isn't this split in javascript working?

From Dev

Exactly the same javascript but one isn't working?

From Dev

Why isn't this Javascript calculator working

From Dev

Javascript isn't working in jsp file

From Dev

Javascript Code isn't working for Date viewing?

From Dev

JavaScript permutations function - why isn't this working?