Created
September 24, 2016 03:01
-
-
Save mojenmojen/19f356e0e617b91aacfddd02f73446a6 to your computer and use it in GitHub Desktop.
Codewars Kata: An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.
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
function isIsogram(str){ | |
console.log( 'string: ' + str ); | |
// length of the string | |
var stringLength = str.length; | |
console.log( 'stringLength: ' + stringLength ); | |
// if the string is empty then it is considered an isogram | |
if ( stringLength == 0 ) { | |
return true; | |
} else { | |
// make a lowercase version of the string since we ignore case | |
var stringLowercase = str.toLowerCase(); | |
console.log( 'stringLowercase: ' + stringLowercase ); | |
// array containing the individual characters in the lowercase string | |
var arrayOfCharacters = stringLowercase.split(""); | |
// loop through the array of characters | |
for ( var i = 0; i < stringLength; i++ ) { | |
console.log( 'i: ' + i ); | |
// get the character from the array with index i | |
var checkChar = arrayOfCharacters[i]; | |
console.log( 'checkChar: ' + checkChar ); | |
// use reduce to count the number of times the character appears in the array | |
var countChar = arrayOfCharacters.reduce( function( n, val ) { | |
return n + (val === checkChar); | |
}, 0); | |
console.log( 'countChar: ' + countChar ); | |
// if this character appears more than once in the array then return false | |
if ( countChar > 1 ) { | |
return false; | |
} | |
} | |
// otherwise if we get this far then the string is an isogram | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment