Alternating Numbers | Function to check if a string is of the form even odd even odd ... ; Function to check if a string is of the form odd even odd even ... ; Function to check if n is an alternating number ; Given number N ; Function call
function isEvenOddForm ( s ) { let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s [ i ] % 2 != 0 ) return false ; if ( i % 2 == 1 && s [ i ] % 2 != 1 ) return false ; } return true ; } function isOddEvenForm ( s ) { let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s [ i ] % 2 != 1 ) return false ; if ( i % 2 == 1 && s [ i ] % 2 != 0 ) return false ; } return true ; } function isAlternating ( n ) { let str = n . toString ( ) ; return ( isEvenOddForm ( str ) || isOddEvenForm ( str ) ) ; } let N = 129 ; if ( isAlternating ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;