Bug in challenge FP sort array with no side effects?

Hello, is there a bug in this challenge - section JavaScript Algorithms & Data Structures subsection Functional Programming: Return a Sorted Array Without Changing the Original Array. ???

I’ve messed with this fairly simple challenge for awhile, trying to figure out why the challenge kept saying I couldn’t hardcode my solution. The only way I was able to pass this challenge was by using the solution given in the “Get Help” page.

My solution worked and was basically the same just a bit more code. I would love it if someone could take a look at this, and tell me if my solution really was / is hardcoded and what the difference might be in what the final solution was, compared to the solution I came up with.

Thanks for anyone that takes a look into this! I would really love some clarification!

This is the solution I came up with -

var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
  // Only change code below this line
    var arr3 = [].concat(arr);

  arr3.sort(
    (a, b)=> {
      if (a === b) {
        return 0;
      } else if (a < b) {
        return -1;
      } else if (a > b) {
        return 1;
      }
    }
  );

  return arr3;


  // Only change code above this line
}
nonMutatingSort(globalArray);

It passed all the tests, except for the one saying I couldn’t hardcode.

The solution provided by FCC follows -

var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
  // Add your code below this line
  return [].concat(arr).sort(function(a, b) {
    return a - b;
  });
  // Add your code above this line
}
nonMutatingSort(globalArray);```

Hello there,

The test for this is here:

assert(!nonMutatingSort.toString().match(/[23569]/g));

So, unfortunately, your variable naming convention arr3 caused the false assertion.

I am surprised yours is the first topic I have seen with this problem.

Hope this helps

2 Likes

Hey! Thanks for the clarification. I kept reworking my solution trying out different ways to get this to work. All along it was my variable name. face palm

Hopefully if anyone does have the same issue, they’ll stumble upon this topic!

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