4

I have this html:

<input type='number' id='length' step='0.1' min='0'; max='5'>Length

and this Javascript

num=document.getElementById('length').value;
if(num==1 || 2 || 3 || 4|| 5){
num='0'+num;
}

My problem is this: while I only want the code inside the brackets to execute if the number from the input is an integer, it also activates if it detects 0.8 or some other decimal. Any Idea why? How do I fix it? Thanks.

  • There are no integers in javascript, only `number`s. Also, when evaluating numbers, all values except 0 equals `true` so `|| 2` equals `true` – Jan Jul 26 '15 at 02:02
  • Your `if` logic is broken. – Ram Jul 26 '15 at 02:03
  • Unless you only want those specific ints, use something like this, rather than a long `if`: http://stackoverflow.com/questions/3885817/how-to-check-that-a-number-is-float-or-integer – DBS Jul 26 '15 at 02:05
  • `min='0';` →`min='0'` – Ry- Jul 26 '15 at 02:17
  • 1
    `num==1 || 2 || 3 || 4|| 5` actually means `(num==1) || (2) || (3) || (4) || (5)`, so your code will always evaluated to true – phuclv Jul 26 '15 at 02:23
  • @LưuVĩnhPhúc yeah. Thanks. Just a bit rusty. First time using Js in a while. I was so used to JQuery, and then here I am working on a project where the circumstances don't permit me to use it. Got a bit spoiled with Jquery –  Jul 26 '15 at 02:48
  • 1
    The `if` statement has nothing to do with jQuery. It's basic JavaScript. – Ram Jul 26 '15 at 02:49
  • @Vohuman I know. I was just saying that I haven't used it in a while. –  Jul 26 '15 at 02:51

4 Answers4

7

To make sure num is a whole number, without having to define all possibilities, use:

if (num % 1 == 0)
Madness
  • 2,730
  • 3
  • 20
  • 29
  • 2
    thanks for the help. I made a stupid error. Bit rusty at JS. I will accept in morning, can't now cause time limit. Thanks! –  Jul 26 '15 at 02:07
2

You should do

if (num == 1 || num == 2 || num == 3 || num == 4 || num == 5)

WRONG - otherwise it will compare 2 with 2 and says it's true for the 4 last 'if' parameters.

CORRECTO - any number in JS is considered as true.

Ted
  • 580
  • 4
  • 22
  • 3
    "compare 2 with 2 "? Any number which is not 0 is considered a truthy value in JavaScript. (I haven't *voted) – Ram Jul 26 '15 at 02:05
  • 2
    Yeah what @Vohuman said is what's actually happening. No I did the voting. The explanation of why the code isn't working is spreading misinformation. – Jan Jul 26 '15 at 02:06
  • this is the correct way to compare, but it doesn't solve the OP's problem, as this only checks for 5 integers but there are much, much more other integers – phuclv Jul 26 '15 at 02:24
  • 1
    @LưuVĩnhPhúc it does solve it because I only needed to check for a few. In case you didn't notice, the input form had a maximum value of five, so I only needed to check for integers 1-5.give him his vote back. –  Jul 26 '15 at 02:53
2

Why:

num==1 || 2 || 3 || 4|| 5

equals to:

(num==1) || 2 || 3 || 4|| 5

so if num is "1" (always a string type), the expression returns true, otherwise 2 (also a truthy value), eventually your if statement always succeeds.

How to fix:

// implicitly converts the string type into number type before comparison
// returns true if it is an integer-like string
num == Math.floor(num) 

So you could do it like this:

if (num == Math.floor(num) && num > 0 && num < 6) {
    // an integer-like string that meets the requirement [1, 5]
}

But remember, the num is still string type now. If you want a number, do:

num = +num
Leo
  • 13,428
  • 5
  • 43
  • 61
0

You have to edit the "If" loop:

if (num == 1 || num == 2 || num == 3 || num == 4 || num == 5)
FlokiTheFisherman
  • 224
  • 1
  • 5
  • 13