0

I am looking for a way to get my array to create a new array, with numbers in reverse without using the reverse method.

e.g.

let numbers = [1, 2, 3]
console.log(reverse(numbers))

should return "[3, 2, 1]".

Thanks

asked Oct 12, 2020 at 11:02
5
  • note: arrays have a reverse method Commented Oct 12, 2020 at 11:03
  • 1
    Why not using the .reserve() method? Commented Oct 12, 2020 at 11:04
  • "I will use an for loop." ok, what is the question? Commented Oct 12, 2020 at 11:06
  • 1
    Please try instead of reverse(numbers) this numbers.reverse(); Commented Oct 12, 2020 at 11:11
  • 1
    const numbers = [1, 2, 3, 4, 5, 6]; const result = numbers.reduce((a, b) => [b].concat(a), []); Commented Oct 12, 2020 at 11:31

3 Answers 3

2

Here. A quick solution with map. returns you new array

let numbers = [1, 2, 3]
let result = numbers.map((el, i, arr) => arr[arr.length - 1 - i])
console.log(result)

answered Oct 12, 2020 at 11:06
Sign up to request clarification or add additional context in comments.

Comments

2

Here is a quick way using a standard for loop

const numbers = [1, 2, 3]
const reverse = [];
for (let i = numbers.length - 1; i >= 0; i--) {
 reverse.push(numbers[i])
}
console.log(reverse);

answered Oct 12, 2020 at 11:07

Comments

2

Short and simple:

let numbers = [1, 2, 3, 4];
const reverse = (array) => array.map(array.pop, [...array]);
console.log(reverse(numbers));

answered Oct 12, 2020 at 11:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.