Skip to content

Instantly share code, notes, and snippets.

@jarhoads
Last active June 5, 2018 01:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jarhoads/3f19359e98de969fbb4e47a5e29e30cd to your computer and use it in GitHub Desktop.
Save jarhoads/3f19359e98de969fbb4e47a5e29e30cd to your computer and use it in GitHub Desktop.
Some ideas on checking for an isogram in javascript
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