Last active
June 5, 2018 01:26
-
-
Save jarhoads/3f19359e98de969fbb4e47a5e29e30cd to your computer and use it in GitHub Desktop.
Some ideas on checking for an isogram in javascript
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
var Isogram = function(w){ | |
var word = w.toLowerCase().split(''); | |
this.isIsogram = function(){ | |
var letters = new Object(null); | |
for(var i = 0; i < word.length; i++){ | |
if(letters.hasOwnProperty(word[i])){ return false; } | |
else{ letters[word[i]] = i; } | |
} | |
return true; | |
}; | |
this.isIsogramRecur = function(){ | |
var checkLetterRecur = function(letters){ | |
var head = letters[0]; | |
var tail = letters.slice(1); | |
if(tail.length === 0){ return true; } | |
else if(tail.indexOf(head) > -1){ return false; } | |
else{ return checkLetterRecur(tail); } | |
}; | |
return checkLetterRecur(word); | |
}; | |
}; | |
var word1 = new Isogram('duplicates'); | |
console.log("word1: " + word1.isIsogram()); | |
console.log("word1 Recur: " + word1.isIsogramRecur()); | |
console.log(''); | |
var word2 = new Isogram('eleven'); | |
console.log("word2: " + word2.isIsogram()); | |
console.log("word2 Recur: " + word2.isIsogramRecur()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment