Gerry
Gerry

Reputation: 5

How to calculate the product of the elements in a Javascript array?

I was looking for a good explanation of calculating product of the elements in one array, but the answers I found are not working in my case. I'm trying to find the product of two arrays and compare them. Found some articles where is written that I can use this function:

    let total = 1; 
    for(let i = 0; i <= array.lenght; i++){
        if(isNaN(array[i])){
            continue;
        }else{
            total *= Number(array[i]);
        }
    }
    return total;
}

No matter what are the values in the array I'm always getting 1 as total. I'm posting the code, so if someone can show me my mistake or tell me some other way to calculate the product, I'll be very grateful.

Here's the code link

Upvotes: 0

Views: 445

Answers (1)

Cheng Adrian
Cheng Adrian

Reputation: 177

First your ... i <= array.lenght; i++ ...
your length misspelled into lenght

Please correct that

Upvotes: 1

Related Questions