Created
May 26, 2013 20:04
-
-
Save jremmen/5653858 to your computer and use it in GitHub Desktop.
js: alpha numeric string pattern test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// recursive function to test if string follows a pattern | |
// of repeating single alpha > numeric or numeric > alpha characters | |
function strnum(s) { | |
function iter(t, i) { | |
if(i > s.length - 1) return true; | |
else { | |
if(t === 1) { | |
if(!isNaN(s[i])) return false; | |
else return iter(2, i += 1); | |
} | |
else if(t === 2) { | |
if(isNaN(parseInt(s[i], 10))) return false; | |
else return iter(1, i += 1); | |
} | |
else return false; | |
} | |
} | |
if(!isNaN(s[0])) { | |
return iter(2, 0); | |
} else { | |
return iter(1, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment