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: ?

Your for statement is wrong.
You have:

for(var x=0;x<segVet[x].length;x++){

segVet[x].length is always going to be 1 when it starts the loop, so it will only look at the first letter. You need to put the length of segVet here and not the length of a specific letter.

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?

No in response to your question. Put a console.log in like the one below and you can see what the value is for x and segVet[x] at the start of each loop iteration. Also, making the change to the for statement will not solve all your problems, but it will at least get you closer. If you use console.log at strategic places in your code, you will be able to see the values of what is getting compared in your if statements for debugging purposes.

HINT: Make the change to the for loop loop I mentioned that you said you already tried and put the following into the console.log on line 5:

console.log(x, segVet[x], primVet.indexOf(segVet[x]));

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”]);

That will work, but another way is to loop through until you a letter that is not in primVet and immediately:

return false

if you make it through the entire loop, that means it never returned false, so just

return true

such as:

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

You could have also use your original line of thinking and add a break statement:

function mutation(arr) {
  var primVet=arr[0].toLowerCase();
  var segVet=arr[1].toLowerCase();
  for(var x=0;x<segVet.length;x++){
    if(primVet.indexOf(segVet[x])<0){
      resp=false;
      break; // this will kick out of the for loop immediately
    }
    else{
      resp=true;
    }
  }
  return resp;
}

One last approach kind of like the last one above is to initialize resp to true in the for statement. It will stay true unless the false condition arises (see below). This allows you to get rid of the else statement in the variation above.

function mutation(arr) {
  var primVet=arr[0].toLowerCase();
  var segVet=arr[1].toLowerCase();
  for(var x=0, resp=true; x<segVet.length;x++){
    if(primVet.indexOf(segVet[x])<0){
      resp=false;
      break; // this will kick out of the for loop immediately
    }
  }
  return resp;
}

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 ?

See the following for making your code more readable in your forum posts.

As far as your 2nd question, you will need to give me an example, so I can figure out the issue.

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

When you say “last 2 statements”, to which ones are you referring?

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

I will break down what the following code does step by step with the 2nd to last test as an example:

mutation([“hello”, “neo”]) should return false.

function mutation(arr) {
  var primVet=arr[0].toLowerCase();
  var segVet=arr[1].toLowerCase();
  for(var x=0, resp=true; x<segVet.length;x++){
    if(primVet.indexOf(segVet[x])<0){
      resp=false;
      break; // this will kick out of the for loop immediately
    }
  }
  return resp;
}

Step 1) Assign the string “hello” to the variable primVet
Step 2) Assign the string “neo” to the variable segVet
Step 3) Enter the for loop and initialize x = 0; resp = true. If x is less than segVet.length then continue to Step 4, otherwise escape out of for loop and continue to next statement after for loop.
Step 4) Since x is currently 0, SegVet[0] is equal to “n”. Check to see if “n” has an index in primVet. Because primVet.indexOf(“n”) evaluates to -1, we enter the “true” part of the if statement and assign resp = false and break out of the for loop and continue to next statement after for loop.
Step 5) Since the next step after the for loop is return resp, we return false, because resp currently has the value false from Step 4.

That is about as detailed of an explanation for why the code above results in the correct result of false for 2nd to last test.

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 !