Isograms: codewars kata confusion

  • Thread starter shivajikobardan
  • Start date
  • Tags
    Confusion
In summary, the code provided is a function called isIsogram which takes in a string and returns true if the string is an isogram (a word with no repeating letters) and false if it is not. The function uses nested loops to iterate through the string and compares each letter to the following letters. If a match is found, the function immediately returns false, otherwise it returns true. However, there seems to be an issue with the code as it is not passing all tests. It is recommended to use debugging techniques, such as logging the progress of the loop, to find and fix the error. Additionally, it is worth noting that JavaScript now has a Set object which can be used for this type of problem.
  • #1
shivajikobardan
674
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
  • #2
Well for one, the code seems to return unconditionally on the first pass of either loop.
 
  • Like
Likes pbuk
  • #3
Think about how many times you have check letters before you can return true.
 
  • #4
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.
 
  • #5
pbuk said:
just log everything to the console and look for it.
there's no error in console.
 
  • #6
got it.
 
  • #7
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 Halc
  • #8
Readers may be interested to know that JavaScript now has a Set object.
 
  • Informative
Likes Ibix

1. What are isograms?

An isogram is a word or phrase that does not contain any repeating letters. This means that every letter in the word or phrase appears only once.

2. How do you determine if a word is an isogram?

To determine if a word is an isogram, you can use the following steps:
1. Convert the word to lowercase to make the comparison case-insensitive.
2. Loop through each letter in the word.
3. Use a counter to keep track of how many times each letter appears.
4. If a letter appears more than once, the word is not an isogram.
5. If the loop completes without finding any repeating letters, the word is an isogram.

3. Why are isograms important in coding challenges like Codewars?

Isograms are important in coding challenges because they test our ability to manipulate and compare strings, which are fundamental skills in programming. They also help us practice problem-solving and critical thinking, as finding solutions for isogram-related challenges can be challenging and require creativity.

4. What are some common techniques used to solve isogram-related challenges?

Some common techniques used to solve isogram-related challenges include:
1. Converting the word to lowercase and using a counter to keep track of letter occurrences.
2. Sorting the letters in the word and checking for duplicates.
3. Using regular expressions to compare and manipulate strings.
4. Breaking down the problem into smaller sub-problems and solving them individually.

5. Can isograms be applied to languages other than English?

Yes, isograms can be applied to languages other than English. The concept of isograms applies to any language that uses a writing system with individual characters or letters. However, the rules for determining if a word is an isogram may vary depending on the language and its specific writing system.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
755
  • Programming and Computer Science
Replies
15
Views
1K
Replies
1
Views
647
  • Engineering and Comp Sci Homework Help
Replies
2
Views
941
  • Engineering and Comp Sci Homework Help
Replies
3
Views
940
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top