I am trying to solve a Javascript challenge on Codewars.
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.
isIsogram( "Dermatoglyphics" ) == true
isIsogram( "aba" ) == false
isIsogram( "moOse" ) == false // -- ignore letter case
My effort is below:
function isIsogram(str) {
var arr = str.split("");
var seen = {};
var out = [];
var length = arr.length;
var j = 0;
for(var i = 0; i < length; i++) {
var item = arr[i].toLowerCase;
if(seen[item] !== 1) {
seen[item] = 1;
out[j++] = item;
}
}
console.log(out.toString.toLowerCase);
console.log(str.toLowerCase);
if (out.toString.toLowercase === str.toLowerCase) {
return true;
}
else {
return false;
}
}
In codewars the result of my
console.log(out.toString.toLowerCase); is undefined
and the result of
console.log(str.toLowerCase); is [Function: toLowerCase].
This means my solution always evaluates to false.
I would appreciate any help to point me in the right direction or highlight my errors instead of giving me the solution so I can learn more effectively. Thanks!
This may be a simpler answer.
function isIsogram(str){
// Turn all letters of the string to lower case and split it into an array.
var letters = str.toLowerCase().split('');
var checkLetters = [];
/* Check to see if the letter appears in the checkLetters array.
If the letter is not already in the array it will push the letter into it. */
letters.forEach(function(letter) {
if(checkLetters.indexOf(letter) === -1) {
checkLetters.push(letter);
}
});
/* Now we have two arrays. If the letters array has non-duplicate letters then
it will be the same length as the checkLetters array. If not, the checkLetters array
will be shorter. */
/* Return true or false depending on whether the lengths of both arrays are equal */
return letters.length === checkLetters.length ? true : false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With