Mutations - questions on differences of if statements and array with index

function mutation(arr) {
var primVet=arr[0].toLowerCase();
var segVet=arr[1].toLowerCase();
for(var x=0;x<segVet[x].length;x++){
if(primVet.indexOf(segVet[x])<0){
resp=false;
}
else{
resp=true;
}
}
return resp;
}

mutation([“hello”, “hey”]);

Here is my code, I don’t understand well how the 1st statement doesn’t evaluate to false since I’m going through the whole second array, and if i exchange, many more evaluate to wrong. I only have this statement to correct. Any enlightening :slight_smile: ?

I have tryed it before, but it evaluates the 2 last statements to wrong. Isn’t segVet[x] simply all letters of its length at the rate of +1 in the for statement?

So on the first occurrence of false return from indexOf() within the for loop, we should exit the for loop and return false, as opposed to when true continue checking the remaining letter in segVet, correct ? As in -

function mutation(arr) {
var primVet=arr[0].toLowerCase();
var segVet=arr[1].toLowerCase();
for(var x=0;x<segVet.length;x++){
console.log(x, segVet[x], primVet.indexOf(segVet[x]));
var res = primVet.indexOf(segVet[x]) == -1 ? false : true;
if (!res) {
x = segVet.length;
}
}
return res;
}

mutation([“hello”, “hay”]);

Hey man, you look like a pro. It’s second time you help me on JS Basic algorithms, I apreciate it. I have posted this topic because I only rely on online content, and want to learn, not copy. I hope that I can have some sort of online conversations more deeper into this. Many thanks I will look more into it.

Thank you for your insights, @rmdawson71 . I thought about the break statement in the for loop but since it was not in FCC tutorial on For Loop (it was in that for Switch statement), I didn’t use it. Not trying to make any excuse here, just that I find FCC curriculum seems to teach the very minimum (probably why the read-search-ask), students need to either already have some basic (gained somewhere else) or be able to gain basic on the side while following thru the curriculum. Forum experts like yourself who spend time explaining the basics help a lot, so thanks a bunch!

One more Q, how do you post the code snippet with indentations using the editor provided ? The editor just removes all indentations in code snippet. Also, how can I prevent the editor from interpreting html tags in the post I don’t want it to interpret ?

Why did the break made the last 2 statements right? I still don’t understand.

I’m referring to the last 2 options which evaluate to wrong.

My 2nd Q refers to - if I want to include any html statement in my post, say, an anchor closed in < > with href, I can’t type the statement as it would appear in html file, the editor will try to interpret it. This is true for many other html statements. How do I stop the editor from being a smart a** here ?

Thank you @rmdawson71, for volunteering your time to help beginners like myself. You are simply wonderful !