Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a better solution #124

Open
lovetingyuan opened this issue Aug 19, 2021 · 5 comments
Open

a better solution #124

lovetingyuan opened this issue Aug 19, 2021 · 5 comments

Comments

@lovetingyuan
Copy link

module.exports = function iseven(n) {
  return n % 2 == 0
}

image

@tmburnell
Copy link

The one thing his does that the above does not do is ... that the number could be typed out. Below might be another possibility.

sEven = (value) => {
  if (isNaN(+value)) {
    return !!value.match(/(?:zero|two|four|six|eight|ten|twelve|fourteen|sixteen|eighteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|hundred|thousand|million|billion)$/gi);
  } else {
    return +value % 2 === 0;
  }
}

@northmatt
Copy link

Ew, modulo. If you're going for the better solution use bitwise.

module.exports = function IsEven(n) {
  return !(n & 1)
}

@kyriosli
Copy link

Bitwise op cannot be used, for example:

isEven(2.2) // true

@northmatt
Copy link

Bitwise op cannot be used, for example:

Nor can modulo operator be used on floats without doing a bunch of extra steps. Dividing 2.2 by two leaves you with 1.1, which is one with a remainder of some sorts.

Not to mention, it seems the general online consensus is that one cannot have an even/odd decimal value. For example 2.3 by our standard rules would be odd, but 2.30 by the standard rules would be even (is the last digit even/odd?). Or whether 2/3 is even/odd (2.66666, last number technically doesnt exist which is undefined, is six which is even, is rounded to 2.67 which is odd if you ignore the fact you can add trailing zeros to make it even, or thought about logically as a fraction which is probably odd). Im not a math expert so who knows how correct that is.

@tuchida
Copy link

tuchida commented Aug 21, 2021

Arithmetic operations on BigInt and Number will trhow a TypeError.

2n % 2 // Uncaught TypeError: can't convert BigInt to number

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants