Help with a basic algorithmic scripting problem

I am going through the basic algorithm scripting at the moment. I know this is not the best way to do it, but I wrote this code as an attempt to solve this problem. Every test passes apart from the test which has duplicate characters in arr[1]. I have looked at the proper solutions and understand now that ive gone a bit overkill and don’t actually need to compare each character, but before moving on I would like to see how I could make this code work with the way I have written it.

Is there a simple change I can make that will make it avoid doing total++ for any duplicate characters?

function mutation(arr) {

let total = 0
let newArr = => val.toLowerCase()) 

for (let i = 0; i < arr[0].length; i++) {
  let char = newArr[1][i]

  if (char == undefined) {

  else if (newArr[0].indexOf(char) > -1) {

if (total == arr[1].length) {
  return true;
} else {
  return false;


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

Challenge: Mutations

Link to the challenge:

Let’s start by cleaning up the code you have so far. Keeping your code clean makes it easier to maintain, find the problems, and get help.

- if (char == undefined) {
-   break;
- } 
- else if (newArr[0].indexOf(char) > -1) {
-   total++
- }
// in this case, is the same as
+ if (newArr[0].indexOf(char) !== -1)
+   total++;
- if (total == arr[1].length) {
-   return true;
- } else {
-   return false;
- }
// is the same as
+ return total == arr[1].length;

So now we have:

function mutation(arr) {
  let total = 0;
  const newArr = => val.toLowerCase());

  for (let i = 0; i < arr[0].length; i++) {
    const char = newArr[1][i];

    if (newArr[0].indexOf(char) !== -1)

  return total === arr[1].length;

Your solution can actually work, the fundamental problem lies on the following lines:

for (let i = 0; i < arr[0].length; i++) {
  const char = newArr[1][i];

What you wrote will get the number of character in the first word, but then you use that index to get characters from the second word.

In the case of mutation(["Mary", "Aarmy"]), arr[0].length is 4. So you’re doing 4 iterations with the indexes 0, 1, 2, and 3. Then you’re using i to select a character from the word Aarmy which is 5 characters. You actually needed the indexes 0, 1, 2, 3, and 4, which you’d have if you got the length of the correct word when creating the for loop.

In other words, you only have to change 1 character , to pass the test.

Hi there @d.heeley2 ,

Allow me to share a few observations I’ve made on your code, as follows:

  1. First, let’s go back to the ‘gist of the challenge’, which says " 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."

  2. Your code is looking the other way; if you would take a second look at your ‘for loop()’, you are using “arr[0].length” as your basis for iterating through each character on “newArr[1]”; this caused you unnecessarily to use several ‘if-else if’ statements as a ‘watchdog’ and a ‘totalizer’, and thus, led to further complications;

  3. Look through the other way:

    1. use ‘newArr[1].length’ as your basis and condition for the ‘for loop()’ iterator; that is, iterate through each character of 'newArr{1] and use it as an ‘argument’ for the ‘indexOf()’ method, while using newArr[0] as your ‘test string’;
    2. If a match is found, loop over to the next character index of ‘newArr[1]’; if all characters find a match in the test string 'newArr[0], return ‘true’.
    3. Else if any mismatch is found, return ‘false’.

Hope you find this helpful. Happy Coding!

Thank you both so much for the help. I see it now.

I sometimes find it difficult to take a step back and evaluate the flow of the code when struggling to figure out where I went wrong. I am going to start writing notes above each code block so that I do not have to track so many variables in my head at once and can look at each component one at a time. That should help!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.