Maximum item in a list

the following function returns ‘a’ as the max. item in the array, but if I change the second line into:
var max=0, the function will return 4, why? max will be equal to ‘a’ anyway and is going to be checked against the rest of the array in both cases, can anyone explain what I am getting wrong?

array = ['a',1,2,3,4]
var max=array[0]
const maxNum=(arr)=>{

array .forEach(
        if(max<item) max=item
return max 
console.log(maxNum (array ))``

It’s because of type coercion.
When you set max=array[0] , effectively max is set to 'a', and the statement if(max<item) will never return true as the operator coerces 'a' into a number which results in NaN , and NaN < any number (or any other operator) will always return false.
In your second case when max=0 , the loop’s first iteration will be false for the same reason above (0 < 'a'). however it will start returning true for the remaining elements in the array.

see topic: type coercion , especially see the list of triggers for implicit numeric conversion

1 Like

Thank you very much for the replay, however I am lost after reading the article because there is nothing about coercing characters into NaNs,
Also I have hard time understanding this:

Why type coercion stop working for the rest of comparisions?

Ok, here is what happens in your for loop, for the first case max=array[0]

Index | Comparison | Outcome | max|

0 | 'a' < 'a' | false | ‘a’ |

1 | 'a' < 1 | false | ‘a’ |

2 | 'a' < 2 | false | ‘a’ |

3 | 'a' < 3 | false | ‘a’ |

4 | 'a' < 4 | false | ‘a’ |

So max is returned which still has a value of ‘a’ which is what is returned, but when you set max=0 in your second scenario

Index | Comparison | Outcome | max|

0 | 0 < 'a' | false | 0 |

1 | 0 < 1 | true | 1 |

2 | 1 < 2 | true | 2 |

3 | 2 < 3 | true | 3 |

4 | 3 < 4 | true | 4 |

Now, max = 4 and that is what is returned, in the first case type coercion interferes for all the indices, in the second case only for index = 0, for all cases a is coerced into a number resulting in NaN and NaN compared with anything else is false, even NaN === NaN is false.

1 Like

Thank you Sir I am deeply grateful for you having all that trouble explaining the problem, wow now I can understand why javascript is dreaded by so many developers.
Do you have some recommendations for beginners to avoid js traps? a lot of materials out there inroduce js as just another programming language while in practice it requires a special kind of understanding.

No trouble at all, helps me think through the concepts as well.
My suggestion is just to build projects and practice, and you will learn as you go, don’t sweat some of these seemingly obscure concepts too much, in time you will get more familiar with them , so just keep on building projects, apps etc… doing those practical things will teach you much more than any book IMHO. When you run into issues that seem weird, as you did with this topic, then google them and/or post them here and you will find plenty of references.

1 Like