Skip to content

Instantly share code, notes, and snippets.

@jremmen
Created May 26, 2013 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jremmen/5653858 to your computer and use it in GitHub Desktop.
Save jremmen/5653858 to your computer and use it in GitHub Desktop.
js: alpha numeric string pattern test
// 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