Isograms: codewars kata confusion

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Confusion
AI Thread Summary
The discussion centers on a JavaScript function designed to determine if a string is an isogram, meaning it contains no repeating letters. The initial code provided fails to pass all tests due to an unconditional return statement within the nested loops, which causes the function to exit prematurely after the first comparison. Participants emphasize the importance of debugging by suggesting logging the loop's progress to identify where the logic fails. Additionally, the conversation highlights the utility of JavaScript's Set object as a more efficient alternative for checking unique characters in a string.
shivajikobardan
Messages
637
Reaction score
54
TL;DR Summary
codewars kata not passing all tests.
https://www.codewars.com/kata/54ba84be607a92aa900000f1/train/javascript
This is my code for it.
JavaScript:
function isIsogram(str) {
  let i, j;
  let str1 = str.toLowerCase();
  for (i = 0; i < str1.length; i++) {
    for (j = i + 1; j < str1.length; j++) {
      if (str1[i] === str1[j]) {
        return false;
      }
      else {
        return true;
      }
    }
  }
}
isIsogram("Dermatoglyphics");

My code isn't passing all tests, why? I feel this works fine.
 
Last edited:
Technology news on Phys.org
Well for one, the code seems to return unconditionally on the first pass of either loop.
 
Think about how many times you have check letters before you can return true.
 
shivajikobardan said:
My code isn't passing all tests, why? I feel this works fine.
Which test is it not passing? Note I already know this, I am asking because YOU need to know; I don't need any help.

When you have identified it, step through the code in your head/on paper/in debug until you get to the point where it fails, or just log everything to the console and look for it.
 
pbuk said:
just log everything to the console and look for it.
there's no error in console.
 
got it.
 
shivajikobardan said:
there's no error in console.
I meant that you should log the progress of your loop to the console. This is how you debug code.
 
Readers may be interested to know that JavaScript now has a Set object.
 
Back
Top