Isograms: codewars kata confusion

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Confusion
Click For Summary
SUMMARY

The discussion focuses on a JavaScript implementation of the isogram problem from Codewars, specifically the function isIsogram. The user's code fails to pass all tests due to an unconditional return statement that exits the function prematurely. The solution involves correctly iterating through the string and checking for duplicate characters without returning true immediately after the first comparison. Utilizing JavaScript's Set object is recommended for a more efficient solution.

PREREQUISITES
  • JavaScript programming fundamentals
  • Understanding of loops and conditional statements
  • Familiarity with debugging techniques in JavaScript
  • Knowledge of JavaScript Set object for unique value storage
NEXT STEPS
  • Research how to implement the Set object in JavaScript for checking unique characters
  • Learn about debugging techniques in JavaScript, including console logging
  • Explore advanced string manipulation methods in JavaScript
  • Study the performance implications of different algorithms for solving the isogram problem
USEFUL FOR

JavaScript developers, coding challenge participants, and anyone looking to improve their debugging skills and algorithmic thinking.

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:
Technology news on Phys.org
Well for one, the code seems to return unconditionally on the first pass of either loop.
 
  • 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.
 
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.
 
  • Like
Likes   Reactions: Halc
Readers may be interested to know that JavaScript now has a Set object.
 
  • Informative
Likes   Reactions: Ibix

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
55
Views
7K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 97 ·
4
Replies
97
Views
9K
  • · Replies 3 ·
Replies
3
Views
2K