简体   繁体   中英

I can't get my reversed array to print into the console.log calling it

I was finally able to get the array copied and reversed instead of replaced and reversed. what can I try next?

 function copyAndReverseArray(array) { array.slice(0).reverse().map(function(reversed) { return reversed; }); } //Don't change below this line const original = [1, 2, 9, 8]; const reversed = copyAndReverseArray(original); console.log(original, '<--this should be [1, 2, 9, 8]'); console.log(reversed, '<--this should be [8, 9, 2, 1]');

I know my reverse function is working, when I console.log the reversed array, directly in the function.

 function copyAndReverseArray(array) { array.slice(0).reverse().map(function(reversed) { console.log(reversed); return reversed; }); } //Don't change below this line const original = [1, 2, 9, 8]; const reversed = copyAndReverseArray(original); console.log(original, '<--this should be [1, 2, 9, 8]'); console.log(reversed, '<--this should be [8, 9, 2, 1]');

How do I get "reversed" to print from the console.log calling it at the bottom without changing the code below the "//Don't change below this line"?

Your code its ok, just dont forget the scope theory.

The map actually reversed the list as you spect and return the reversed list, but that result only live into the fuction scope (copyAndReverseArray), so you need to return that value again for got it in a superior scope: global scope in this case. If not return the result, you will continue to have an undefined value

so, try this:

function copyAndReverseArray(array){
    return array.slice(0).reverse().map(function (reversed) {
       return reversed
    });
}

And then, you can assign the result to a var as you have been trying

const original = [1, 2, 9, 8];
const reversed = copyAndReverseArray(original);
console.log(original, '<--this should be [1, 2, 9, 8]');
console.log(reversed, '<--this should be [8, 9, 2, 1]');

You need to return in copyAndReverse what you return from the map callback.

function copyAndReverseArray(array){
  return array.slice(0).reverse().map(function (reversed) {
     return reversed;
  });
}

If you need a simpler solution just apply the spread operator syntax like so

function copyAndReverseArray(array) {
  return [...array].reverse();
}

Just like the first approach , this won't change your original array (the one you pass as a parameter).

And just for completeness sake, beware of this:

function copyAndReverseArray(array) {
  return array.reverse();
}

As it will also affect the original array, the one you pass as a parameter. For example:

var arr1 = [1, 2, 3];
var arr2 = copyAndReverseArray(arr1);
//Now, arr1 == arr2. Check it with:
console.log(arr1);
console.log(arr2);

Your code is almost correct. You are missing return . Also your map() is doing nothing. You can safely remove the map() part.

 function copyAndReverseArray(array){ return array.slice(0).reverse(); } //Don't change below this line const original = [1, 2, 9, 8]; const reversed = copyAndReverseArray(original); console.log(original, '<--this should be [1, 2, 9, 8]'); console.log(reversed, '<--this should be [8, 9, 2, 1]');

Try using the array spread operator to clone the original array without mutating it.

function copyAndReverseArray(array) {
  return [...array].reverse();
};

Does this work for you?

function copyAndReverseArray(array){
    reversedArray = Object.assign([],array)
    return reversedArray.reverse()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM