Check number is Odd or Even [JS]

var oddoreven = function (n = false, type = 'odd') {
  var time = !n ? new Date().getDay() : Number(n);
  
  if (!/^-{0,1}\d+$/.test(time)){
    alert('arguments is not number, please remove quote');
    return;
  }
  hasil = time % 2;
  return hasil == (type == ('odd' || 'ganjil') ? 1 : 0);
}
Test
document.write('isOdd / Ganjil : ' + oddoreven(false, 'odd'));
document.write('<br/>');
document.write('isEven / Genap : '+oddoreven(false, 'even'));
Output
isOdd / Ganjil : true
isEven / Genap : false

Subscribe to receive free email updates:

0 Response to "Check number is Odd or Even [JS]"

Post a Comment

Bila Ada posting yang kurang, atau error atau yang lainnya, silahkan tinggalkan komentar agar artikel/post di perbaiki.
Jangan Lupa Cek Box "Notify Me" agar tahu komentar kamu dibalas oleh saya.

If there are any posts that are missing, or error or anything else, please leave a comment for the article / post to be fixed.
Do not Forget Check Box "Notify Me" to know our comments replied by me.