Why does my isogram checker return true too early?

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Confusion
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 2K views
shivajikobardan
Messages
637
Reaction score
54
TL;DR
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:
Physics news on Phys.org
Well for one, the code seems to return unconditionally on the first pass of either loop.
 
Reply
  • Like
Likes   Reactions: pbuk
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.
 
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.
 
Reply
  • Like
Likes   Reactions: Halc
Readers may be interested to know that JavaScript now has a Set object.
 
Reply
  • Informative
Likes   Reactions: Ibix