Functional Programming - Return a Sorted Array Without Changing the Original Array

Tell us what’s happening:

Describe your issue in detail here.
hi, there, my code was showed in correct order in chrome console, but somehow it won’t pass question 6, 7? ? Does anyone know what’s the problem?

Your code so far

const globalArray = [5, 6, 3, 2, 9];

function nonMutatingSort(arr) {
  // Only change code below this line
  let newArr=[...globalArray];
  return newArr.sort(function(a,b){return a-b});

  // Only change code above this line
}

nonMutatingSort(globalArray);

Your browser information:

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

Challenge Information:

Functional Programming - Return a Sorted Array Without Changing the Original Array

You must not reference the global variable.

1 Like

Inside the function you should only use variables either defined inside the function or passed in as arguments.

As an example:

function abc(def) {
   let a = [...def];
   //manipulate a somehow
   return a;
}

abc(ghi)

The call abc(ghi) is calling the function abc and passing the variable ghi.
Inside the function abc we can only use the variable a (because it is defined inside the function) and the argument passed in def.

In this case, there is no real difference but whenever you see a function accept arguments you should always use the parameters. You can’t know what might have happened to the arguments passed to nonMutatingSort before you use them inside the function.

initial data
function definition
initial data manipulated somehow
function is called and passed the manipulated initial data

As an aside and not that you can use it with the challenge, but we now have access to some nice non-mutating to methods, including toSorted.

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