← Back to challenges

The Alternating Numbers

JavaScriptHardnumbersvalidation

Instructions

In this challenge, you have to establish if an integer num is Alternating. To be Alternating, num must be positive and every digit in its sequence must have a different parity than its next and its previous digit.

Given an integer num, implement a function that returns true is num is an Alternating number, or false if it's not.

Examples

isAlternating(123) ➞ true
// 1 is odd, 2 is even, 3 is odd

isAlternating(67) ➞ true
// 6 is even, 7 is odd

isAlternating(2380) ➞ false
// 2 is even, 3 is odd, 8 is even, 0 is even

isAlternating(75) ➞ false
// 7 is odd, 5 is odd

Notes

  • A single-digit number is trivially considered Alternating, given the absence of neighboring digits.
  • The first digit has to be compared only to its next neighbor, and the last digit has to be compared only to its previous neighbor.
  • Every non-positive integer must be treated as false.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.