-1

I'm trying to reverse the contents of an array. My approach works well when the contents of the said array are of same type. but once they're of different types it just doesn't work.
Constraint is that i can't use the .reverse() method and if possible, without creating a new array.
The answer in this question is close to what i want but i don't understand it.

Here is my code...

reverse.js

#!/usr/bin/node
exports.rev = function (list) {
  for (let i = 0, j = (list.length - 1); i <= (list.length / 2); i++, j--) {
    [list[i], list[j]] = [list[j], list[i]];
  }
  return (list);
};

main.js

#!/usr/bin/node
const rev = require('./reverse').rev;

console.log(rev([1, 2, 3, 4, 5]));
console.log(rev(["School", 89, { id: 12 }, "String"]));

Expected:
[5, 4, 3, 2, 1]
["String", { id: 12 }, 89, "School"]

What I got:
[5, 4, 3, 2, 1]
["String", 89, { id: 12 }, "School"]

3
  • Probably you're swapping the central elements twice, and swapping them twice is like not swapping them at all. You should fix it replacing i <= (list.length / 2) with i < (list.length / 2), even if I didn't test it yet Commented Sep 9, 2022 at 9:48
  • This also explains why reverting an odd number of elements doesn't cause any problem (in your first example you're swapping 3 twice, which is fine) Commented Sep 9, 2022 at 9:49
  • @ChristianVincenzoTraina Oh, yeah! This works! I never imagined that will be the source of the problem. Thanks Commented Sep 9, 2022 at 9:56

5 Answers 5

1

I've found out that your code almost works. You just need to modify the condition a bit to

i < (list.length / 2) //not `<=`

function rev(list) {
  for (let i = 0, j = (list.length - 1); i < (list.length / 2); i++, j--) {
    [list[i], list[j]] = [list[j], list[i]];
  }
  return (list);
};

console.log(rev([1, 2, 3, 4, 5]));
console.log(rev(["School", 89, { id: 12 }, "String"]));

Sign up to request clarification or add additional context in comments.

3 Comments

I just modified my question to not clone a new array
Not really a requirement though, but what if i wanted this without having to use an extra array.
Already posted before i saw your comment
0
let array = [1, 2, 3, 4, 5]
let reverse = [];

for (var i = array.length - 1; i >= 0; i--){
    reverse.push(array[i]);
}

1 Comment

In this way you're creating a new array, not what OP wants
0

You can try with a for loop like this:

let arr = ["School", 89, { id: 12 }, "String"];

let newArr = [];

for (let i = arr.length - 1; i >= 0; i--) {
  newArr.push(arr[i])
}
console.log(newArr);

Expected output: ["String", [object Object] { id: 12 }, 89, "School"]

Comments

0

You can user a recursion to reverse

You can use Spread operator(...) along with distructure assignment for that.

function rev(arr) {
  const [first, ...rest] = arr
  return arr.length > 1 ? [...rev(rest), first] : arr
}

console.log(rev([1, 2, 3, 4, 5]));
console.log(rev(["School", 89, { id: 12 }, "String"]));

const [first, ...rest] = arr is a shortcut for:

const first = arr[0]
const rest = arr.slice(1)

However, ...rev(rest) will spread items of returned array and spread them. So, [...rev(rest), first] will keep the output of rev first and then push first at the end of array.


If you are comfortable with mutation of original array, try this

function rev(arr) {
  return arr.length > 1 ? [arr.pop(), ...rev(arr)] : arr
}

console.log(rev([1, 2, 3, 4, 5]));
console.log(rev(["School", 89, { id: 12 }, "String"]));

2 Comments

I'm new to JS so i don't really understand what ... does but i'm interested in this method.
Updated my answer with explanation. Let me know if you have more queries regarding it
0

Already good answers (and examples of arrays) here

My try with native methods:

const rev = (arr) => {
  len = arr.length;
  for (i = 1; i<= len; i++) arr.splice(len-i,0,arr.shift());
}

const array = ["School", 89, { id: 12 }, "String"];

console.log("original",array);
rev(array);
console.log("reversed",array);

Comments

Your Answer

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.