Mutations, mutations, mutations galore

Tell us what’s happening:
So I’ve been at this one for about 2 hours, and I had to use the hint section because I got stuck. From what I understand I need to convert this array to a string and lowercase all the characters. Then i am running the entire string length through a for loop to see if indexOf the string matches the arr? is this correct? I am getting partial completion on the challenge but certain things are returning true when they shouldn’t.

Your code so far


function mutation(arr) { 
let stringForm = arr.toString().toLowerCase();
let newSplit = stringForm.split("");
console.log(newSplit);  
for(let i = 0; i < newSplit.length; i++){
     if(newSplit[i].indexOf(0)){
       return true;
     } else {
       return false; 
     }
 }
}
console.log(mutation(["voodoo", "neo"]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Mutations

Link to the challenge:

/*
 * Return true if the string in the first element of the array 
 * contains all of the letters of the string in the second 
 * element of the array.
*/

let source = arr[0] // first word
let target = arr[1] // second word

// for letter in source:
//     targetContainsLetter = True or False

// if targetContainsLetter is False:
//     return False

@CodingDutchman hey,

looks like youve got it mixed up a bit, what your supposed to do is see if all the letters in the second item in the array arr[1] are in the first item in the array arr[0] so

this would return false because voodoo doesn’t have a n or an e in it

Oh no so should I start from scratch. I feel like i shouldn’t have converted the arrays to strings?

@CodingDutchman

yeah you dont need to convert it to a string because you need them separate so you can compare them, also

and this above wouldnt work anyway because its only returning true if one of them matches and you need to return true if all the letters in arr[1] are in arr[0]

I started by retrieving the two strings
then made them lowercase,
strings are array like objects
so you can iterate through them
with a for( ; ; ) loop,
you can also use the includes
method on string s.
Enough clues ?

I’m sorry Guys, i appreciate the responses, but I am getting a bit more confused. @Da_vey , you are saying the arrays need to be converted to lowercase. But to use .toLowerCase() they have to be a string. @biscuitmanz you are saying that it doesn’t need to be converted to a string. I don’t understand which is correct. The result of all my testing is that I get only partial challenge correct no matter which way I attempt this.

  let stringify = arr.toString().toLowerCase();  
  console.log(stringify); 
   for(let i = 0; i < stringify.length; i ++){
          if(stringify.includes(arr)){
            return true; 
          } else {
            return false; 
          }
      }
    }
console.log(mutation(["floor", "for"]));

I am pretty lost on this one, guys. Super bad imposter syndrome going on.

hey @CodingDutchman

What i meant was they are already strings, you have one string “floor” and another “for”

so all you need to do is access them you dont need to use .toString() you can access them like this

let string1 = arr[0]
let string2 = arr[1]

console.log(string1) // "floor"
console.log(string2) // "for"

then you need to compare them

to save confusion you could use destructuring so that there is no arr and just 2 strings like this…

function mutation([ string1, string2 ]) { 

console.log(string1) // "floor"
console.log(string2) // "for"

}
console.log(mutation([["floor", "for"]);

:grinning: hope this helps

I made no mention of converting arrays

I said strings are “array like” objects

Thanks man. What doesn’t make sense is i know i have to use a for loop(well, that’s what the hint section says) to iterate through the array. So what doesn’t make sense is I’m comparing string1(arr[0]), to string2(arr[1]).

I use a conditional statement to test that. But when I am looking through a for loop i’m looking through the arr.length. I’m never using that arr in my condition since they are already destructured in string 1 and 2…Am I totally off base here?

function mutation(arr) {
  let string1 = arr[0].toLowerCase();
  let string2= arr[1].toLowerCase();
  console.log(string1); 
  console.log(string2); 
  for(let i = 0; i < arr.length; i ++){
      if(string2.indexOf(string1)){
        return true; 
    } else {
      return false;
        }
      } 
    }

console.log(mutation(["hello", "Hey"]));

You want to loop over the characters in the string. Test if every character in the second string can be found in the first.

okay so i have to split the strings

No, you can access characters of strings with an index, just like items in an array.

const str = "hello";
for (let i = 0; i < str.length; i++) {
  console.log(str[i]);
}

Which outputs:

h
e
l
l
o

Better yet, you can just use a for…of loop here.

const str = "hello";
for (const char of str) {
  console.log(char);
}

Which does the same thing as the code above.

the for of loop for me is admittedly simpler, which is why I would like to figure it out in a regular for loop. Why is that when I put my string2 through a for loop i am only getting the return of “n” for string2[i]?

function mutation(arr) {
  let string1 = arr[0].toLowerCase();
  let string2= arr[1].toLowerCase();
  console.log(string1); 
  console.log(string2); 
  for(let i = 0; i < string2.length; i ++){
    console.log(string2[i]);
        if(string2.indexOf(string1)){
        return true; 
    } else {
      return false;
          }
        }
      } 
    
console.log(mutation(["hello", "neo"]));

Why am I only getting “n”

Because you exit the function with the return statement in the first iteration of the for loop.

I don’t follow what it is you want to check with your if statement. You need to check if the first string contains the character you pulled out from the second string.

Ahh, that makes sense. I thought a conditional would be the optimal way to find a true false statement. I assume that is now wrong.

You definitely want a conditional, but you don’t necessarily want to return from the function. In the case that the first string contains the first letter of the second string, then you want to move on to the second letter. If the first string contains the second letter of the second string, then you want to move on to the third letter…

If you make it to a character that isn’t contained in the first string, then you can immediately return because there’s no need to check the remaining characters of the second string (only one missing character is enough to know that you should return false).

1 Like

Okay! So, I understand now that if i do a return it will immediately end once the first value is found, correct? However If I do a conditional without a return statement I’m going to get undefined. Also, If i try to return the original array it’s never going to evaluate to true or false.

function mutation(arr) {
   let string1 = arr[0].toLowerCase();
    let string2 = arr[1].toLowerCase();
    for(let i = 0; i < string2.length; i ++){
      console.log(string2,string1)
        if(string2.indexOf(string1) === true){
            }
          }
        }
       
    
console.log(mutation(["hello", "neo"]));

On the first iteration of the loop, when you plug in the values this statement looks like this:

if ("neo".indexOf("hello"))

Ie, it’s checking if the string “neo” contains the string “hello”.

You instead want to check if the string “hello” contains the letter “n”.

You might want to check out this String method as well.

1 Like

I apologize. I just can’t seem to get this one. I’ve been working on it for 3 days and feel hopeless. The information makes sense during the course, and when I get to algorithm scripting, It’s like I retained nothing. I can’t make heads or tails of understanding these. I think I have to step away from it for a bit or something because I just get more frustrated with myself.

function mutation(arr) {
   let stringToCheck = arr[0].toLowerCase();
    let stringToMatch = arr[1].toLowerCase(); 
    for(let i = 0; i < stringToCheck.length; i ++){ 
        console.log(stringToCheck); 
        if(stringToCheck[i].includes(stringToMatch[i]) === true ){ 
                }
              } 
           }

       
    
// console.log(mutation(["hello", "neo"]));
console.log(mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]))

I can’t even make a return statement…