I am writing a function that returns false if characters are repeated in a string and true otherwise.
The function works correctly when the condition is true, but when it’s false, both true and false values appear in the console, so there is obviously something wrong with the else part of the function.
Could someone help please?
let tempArray = str.split("");
tempArray.sort();
for (let i = 0; i < tempArray.length; i++) {
if (tempArray[i] == tempArray[i + 1]) {
console.log(false);
break;
} else {
console.log(true);
}
}
}
in the function I have below, the false value is in the loop and the true value outside of it (as you said), but the true value is showing up regardless of whether the condition is true or not as there is no condition attached to it.
Have I misunderstood something? Code below…
function isIsogram(str) {
let tempArray = str.split("");
tempArray.sort();
for (let i = 0; i < tempArray.length; i++) {
if (tempArray[i] == tempArray[i + 1]) {
console.log(false);
break;
}
}
console.log(true);
}
I found the solution…below if anyone wishes to see it…thanks for all the help…
function isIsogram(str) {
let result = true;
let tempArray = str.split("");
tempArray.sort();
for (let i = 0; i < tempArray.length; i++) {
if (tempArray[i] == tempArray[i + 1]) {
result = false;
break;
}
}
console.log(result);
}