-2

Hi can somebody tell me why the output to my function defaults to even when you insert over 17 numbers? It's probably super simple, please go easy on me!

function oddOrEven(number) {
  var number = document.getElementById('number').value;
    if(number % 2 != 0) {
    document.getElementById('demo').innerHTML = "Odd";
    } 
  
  else {
    document.getElementById('demo').innerHTML = "Even";
    } 
  if (number.length === 0) {
    document.getElementById('demo').innerHTML = "Odd / Even";
  }
}
danboswell
  • 17
  • 1
  • 1
    Provide complete code. – kind user Apr 22 '17 at 21:00
  • 3
    Your function is written with `number` as a parameter, but the first thing the function does is overwrite the parameter value. How is the function called? – Pointy Apr 22 '17 at 21:00
  • 3
    Also what does "over 17 numbers" even mean? 17 numbers where? – Pointy Apr 22 '17 at 21:01
  • Also, what is the typeof `number`? You check a `number.length` property which doesn't make sense if `number` is a... number. – heylookltsme Apr 22 '17 at 21:01
  • 1
    @Tushar but that won't matter because it's used in the `%` operation only, and in fact that'd cause an error in the `.length` test. – Pointy Apr 22 '17 at 21:02
  • 1
    @heylookltsme it's not a number. It's initialized from a DOM element `.value` property, which means it's a string. – Pointy Apr 22 '17 at 21:02
  • 2
    I'm guessing "over 17 numbers" means numbers with more than 17 digits, which is [above integer accuracy](http://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin). – JJJ Apr 22 '17 at 21:04
  • @danboswell when you say "17 numbers" do you mean "17 **digits**"? – Pointy Apr 22 '17 at 21:05
  • 1
    If you want to test huge numbers just take a the last digit of the string and test that one. – Thakkie Apr 22 '17 at 21:06
  • Yes 17 digits, it looks like @JJJ might possibly have the explanation. Thank you all! – danboswell Apr 22 '17 at 21:06

1 Answers1

-1

You can simplify this whole thing. If you are always grabbing the input with id 'number' you don't need to pass a param, and then after a simple test you can inline the answer you want:

function oddOrEven(){
   var val = document.getElementById('number').value;
   var number = parseInt(val, 10);
   // if it's not a valid number, you'll have NaN here which is falsy
   if (number) {
      document.getElementById('demo').innerHTML = (number % 2) ? "Even" : "Odd";
   }
}

All that said, I just caught that you're talking about 17 digits (thanks to @JJJ's comment) rather than using the function more than once. The problem in this case is that JS integers have a size limit. If you parse anything larger it returns a number you're not going to expect. There are a lot of discussion of general handling of very large numbers here: http://2ality.com/2012/07/large-integers.html, but for your modulus problem you could take the last digit and check if that's odd or even like so:

function oddOrEven(){
   var val = document.getElementById('number').value;
   var number = parseInt(val, 10);
   // if it's not a valid number, you'll have NaN here which is falsy
   if (number) {
      var lastDigit = val[val.length-1];
      document.getElementById('demo').innerHTML = (parseInt(lastDigit, 10) % 2) ? "Even" : "Odd";
   }
}
Paul
  • 35,689
  • 11
  • 93
  • 122
  • Thanks for sharing. It's nice to see how I can refine my code. Much appreciated! – danboswell Apr 22 '17 at 21:09
  • Folks who are hitting the - button should provide comments so that I know what the issue is and can correct it. – Paul Apr 22 '17 at 21:10
  • The problem is *"my function defaults to even when you insert over 17 [digits]"* – this code has the exact same problem. – JJJ Apr 22 '17 at 21:12
  • @JJJ Updated my answer to account for that, thanks for pointing it out. – Paul Apr 22 '17 at 21:22