Tuesday, 19 October 2021

Codewars Coding Challenge: ISOGRAMS


Insograms

A isogram is a word that has no repeating letters, consecutive or non consecutive.


Challenge:


To   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.


"Dermatoglyphics" --> true
"aba" --> false
"moOse" --> false (ignore letter casing)

Solution:

In this blog, the above mentioned coding challenge will be discussed.

const insogram = function (str) {
str = str.toLowerCase();

for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j < str.length; j++) {
if (str[i] === str[j]) return false;
}
}
return true;
}
console.log(insogram("thilip"));// False
console.log(insogram("Red"));// True
console.log(insogram("Budhha"));// false
console.log(insogram("BaTMan"));// false

The above code is the solution for the challenge.  We have created a function insogram to identify the given word has a repeating letters. To check the given string, the string is looped twice to compare the the each letter to every other letter. If there is any letter repeats itself it it will return false, if there is no letters repeating, it will return true once the loop is completed.

The above challenge is to test and develop our looping skill and the string.

Happy coding!!!

Have a good day!!!  





No comments:

Post a Comment

Array Destructuring

  Destructuring  Destructuring is an ES6/ES2015 feature. Destructuring allows us to break down the complex data structure into a simple data...