-2

How should I write an if statement which is true only for a given range e.g. return true for x if (3 < x < 9) ?

if ((myNum > 3) || (myNum < 9))
{ 
    document.getElementById("div").innerHTML = "<button>Hello World</button>";
}
Gas
  • 17,601
  • 4
  • 46
  • 93
Thomas
  • 43
  • 1
  • 4
  • 7
  • 6
    You want to use && not || – xDaevax Sep 10 '14 at 20:20
  • 1
    possible duplicate of [check if a number is between two values](http://stackoverflow.com/questions/14718561/check-if-a-number-is-between-two-values) – xDaevax Sep 10 '14 at 20:23
  • using || means 'or' which means either condition can be true to return true. Basically, any number you use will return true in your current setup. – GeekByDesign Sep 10 '14 at 20:26
  • I'm going to leave this open (not close it) because the choice of duplicates is so poor. The duplicate is so poor because (1) it has 0 votes (it is not deemed a "good" question); and (2) it was closed as "lacks minimal understanding". However, Stack Overflow welcomes less experienced programming enthusiasts, so folks like Thomas should get an answer. Also see [Could we please be a bit nicer to new users?](https://meta.stackexchange.com/questions/9953/could-we-please-be-a-bit-nicer-to-new-users). – jww Sep 10 '14 at 21:45

2 Answers2

1

AND not OR

if (myNum > 3 && myNum < 9) { 
  document.getElementById("div").innerHTML = "<button>Hello World</button>";
}
Chris Caviness
  • 586
  • 3
  • 10
1

Use correct operator || is OR operator, use AND operator &&

if ((myNum > 3) && (myNum < 9))
{ 
    document.getElementById("div").innerHTML = "<button>Hello World</button>";
}

Cheers !!

Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42