Results 1 to 3 of 3

  1. Post

    Any ideas on this? Debugging vanilla JS

    number = 31306
    maxValue = 100000000

    if number > maxValue somehow equals true.

    Surely it can't be a dataType problem because it passed the number < minValue (=1) in the step prior.

    Name:  Screenshot from 2019-10-10 09-28-51.png
Views: 85
Size:  27.6 KB

  2. Post
    Ah, found it. If either are a number, it will convert both to numbers, but if they're both strings it will do a string comparison.

    value on input elements is always a string. While that won't be a problem initially, when you're comparing this.value to the 100 you've put in the array to start with, you then push this.value into the array as-is (as a string). That means later, you'll end up comparing that stored string with another this.value value, which is also a string. If either operand to > is a number, it will coerce the other operand to number (the way + does, see below), but if both operands are strings, it will do a lexical comparison, not a numeric one, and "100" is indeed < "99" because "1" is < "9".

    So you want to convert this.value to a number early on, and then use that number both when comparing and when pushing into your array. You have many ways to do that:
    Didn't have that problem before because the values were stored as numbers in an array, and I've now brought them into classes then cut them out into strings so I can set the limits on the input themselves.

  3. Post
    Yeah man, beware the weakly typed language, where 'c' != 0, but ' ' == 0