Questions on Mutations

Tell us what’s happening:
I am trying to make 2D array like this:
[[false, false, false, false],
[false, false, false, false],
[false, false, true, false],
[false, false, false, false],
[false, false, false, false],
[false, false, false, false],
[false, false, false, false]]

and again, check which values each sub array have:
[false, false, true, false, false, false, false]

finally, check once more whether the array has at least one false. if so, return false ore return true.

but when i try to make this arrays in the loop, syntax error occur.
is there any other logic to solve this problem?

Your code so far

function mutation(arr) {
  var firstArr=arr[0].toLowerCase().split("");
  var secondArr=arr[1].toLowerCase().split("");
  var result=[];
  var answer=[];
    
  for(i=0;i<firstArr.length;i++) {
    for(n=0;n<secondArr.length;n++) {
      result[i]=secondArr[n]==firstArr[i]? true:false;
   }
  }
  
  return answer;
}

mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]);

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/mutations

You might find it helpful to break up your function into smaller parts.

For example, you could write a function to check if character c is inside string one (using a single loop) returning true or false.

One can then use the new function and loop over the characters in string two (using another single loop), return true if all characters are in.

This is much easier to reason about than making a multidimensional array!

Tell us what’s happening:
Basically, my logic for solving this problem is following:

  1. check each letters of second array exist in the first array
  2. give true if there are same letters
  3. give true if every values of answer is true, if not, give false

but, in some case, It makes problems.
any advice and critics will be appreciated.

Your code so far

function mutation(arr) {
  var firstArr=arr[0].toLowerCase().split("");
  var secondArr=arr[1].toLowerCase().split("");
  var result=[];
  var answer=[];
    
  for(i=0;i<firstArr.length;i++) {
    for(n=0;n<secondArr.length;n++) {
      result[i]=secondArr[n]==firstArr[i]? true:false;
      answer[n]=result.indexOf(true)!==-1? true:false;
    }
  }
  
  return answer.indexOf(false)!==-1? false:true;
}

mutation(["Mary", "Aarmy"]);

Your browser information:

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

Link to the challenge:

There is usually three general steps to solving a programming problem/challenge.

  1. Determine what is the problem/challenge.
  2. Abstract away the details down to its bare parts of the core problem.
  3. Tackle the abstracted problem with current knowledge.

I feel like you “took a wrong turn” around step 1 and 2. You said your trying to make a 2d array of boolean values from the mutations problem. I do not believe this is the correct way to abstract away the problem. Thus continuing with your approach will result in a program that doesn’t solve the original problem, thus any work done at step 3 is for naught if your work done at step 1 and 2 is incorrect.

This doesn’t mean step 3 isn’t important. Since you need to rely on your current knowledge to formulate a solution, it helps to understand what tools are available to you, so you can use different tools to solve the same problem. As a hint, the .indexOf method should do most of the work for this challenge.*

I highly recommend you read about the function on mdn:

finally, its probably a good idea to not over complicated things.
result[i] = secondArr[n] == firstArr[i] ? true : false
You should never need to use the ternary to return a boolean from an expression. This code is the same
result[i] = secondArr[n] == firstArr[i];

But the above code is mentioned only as an example of what to avoid in the future, and technically not required to solve this challenge. (Look into the mdn doc posted above to further understand the .indexOf function for strings.)

* When I say the indexOf method should do most of the work, I’m saying don’t do work it can do for you. Work smarter, not harder :smiley: