Find if variable is divisible by 2

前端 未结 12 519
梦谈多话
梦谈多话 2020-12-07 19:22

How do I figure out if a variable is divisible by 2? Furthermore I need do a function if it is and do a different function if it is not.

12条回答
  •  生来不讨喜
    2020-12-07 19:59

    You can use the modulus operator like this, no need for jQuery. Just replace the alerts with your code.

    var x = 2;
    if (x % 2 == 0)
    {
      alert('even');
    }
    else
    {
      alert('odd')
    }
    

提交回复
热议问题