Mutations needs HELP

Mutations needs HELP
0

#1

I’ve been stuck on this one for a little while. Challenge asks me to see if param#2 letters in string are found in param#1 (not in any order). What I’m attempting to do here is cycle it through a for loop, with the loop running [i] through each indexable slot in param2, and checking them against the indexes in param1 for a match. If statement says that if true then return “true” if not “false”. I know im doing something wrong but not sure which thing or things are wrong.

Your code so far

    function mutation(arr) {
    for (var i = 0; i < arr[0].length; i++) {
    arr[0].indexOf(arr[1][i]);
  
     if (arr[0].indexOf(arr[1][i]) !== -1) {
      return "true";
      }}
      return "false";
      }
 mutation(["hello", "hey"]);

Your browser information:

Your Browser User Agent is: ```Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:54.0) Gecko/20100101 Firefox/54.0.1 Waterfox/54.0.1

Link to the challenge:


#2

As soon as a return statement is hit, execution stops and the function is exited. You are returning true if the first letter is found.


#3

Feel like I’m overthinking this. Maybe I should store results .push’ed into a variable? Then compare that one to the param0? I was thinking of doing this but that would bring me to where I started.


#4

Here is the basic algorithm for this challenge:

Iterate through each letter of arr[1] and check if each letter is in arr[0]. If a letter in arr[1] is not found in arr[0] return false. If all letters in arr[1] have been traversed, this means all letters were found, so return true.


#5

I think I figured out a lot of my problem. This site will tell me that my method is not a function. Well it should be. It’s a method, therefore you could say it’s a function right? (May be wrong there but maybe not). So alot of my problem is just that I’m new (i’m an electrician and I’ve been coding for a month or two but I really think it comes natural). For instance, just lack of familiarity with certain things ( I realized I was trying to use .push to populate an empty string). It kept saying “not a function”, which got me nowhere. Then I changed my variable to be pushed to an empty ARRAY and it worked fine. I think I will get the hang of this, and I never give up. Thank you all for your help and when I finish this I will post my results.


#6

Ok. I think it’s just syntax this time. I’ve went through and re-did my code based on rmdawson71’s algorithm logic. What I’ve come up with, if you individually enter in the test parameters, will fntion properly. For some reason it wont pass any of the tests when you “run tests”. I think I’ve either got it and its hung up on something minor, or something worse is wrong. What do you think? I do acknowledge I was doing a few things pretty seriously wrong before.


#7

OOPS here is my code:

function mutation(arr) {
  var cuddler = [];
  var wiggler = [];
  var potato = [];

  for (var j = 0; j < arr[0].length; j++) {

    wiggler.push(arr[0][j].toLowerCase());
  }

  for (var q = 0; q < arr[1].length; q++) {

    potato.push(arr[1][q].toLowerCase());
  }

  for (var i = 0; i < potato.length; i++) {
    wiggler.indexOf(potato[i]);


    if (wiggler.indexOf(potato[i]) == -1) {
      cuddler.push(potato[i]);
      return "false";

    }
  }
  return "true";
}
mutation(["voodoo", "no"]);

#8

Your first solution was actually very close. You were returning true as soon as you found a matching letter and only returning false after checking every letter. What you want to do is return false as soon as you hit a letter that isn’t found and only return true if you make it through the whole string.


#9

I went back to that version. I changed the logic around so it matches what you said. Still getting stuck on the fact that this challenge is not case sensitive. I didn’t even see that part untill later :smile: .In the second version of my code I was able to reconcile that. Ill put them together an see what happens


#10

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard.


#11

Remember that return “false” is not the same as return false. The 1st returns as string value and the 2nd returns a boolean value.


#12

I got it! Knew it was something simple. Thanks you guys for all the help. I’m getting a lot better with practice. I will read up on the backticks.


#13

Glad you got it all straightened out. I wanted to give you a suggestion of how to make your code more readable. Instead of using names like “potato” or “cuddle” for your variables, give them names which describe the values they hold. The same goes with function names. Function names should describe what the function does.

For an example of readable variable names, I will use a solution to the Mutations challenge. I have hidden the solution to avoid spoiling it for someone else. Just mouse over and click the blur to see what I have written.

function mutation(arr) {
  var strToCheck = arr[0];
  var charsToFind = arr[1];
  for (var i = 0; i < charsToFind.length; i++) {
    if (strToCheck.indexOf(charsToFind[i]) == -1) {
      return false;
    }
  }
  return true;
}

#14

Thanks again. I will work on my presentation. Appreciate you letting me know.