sakmario
sakmario

Reputation: 43

Reverse numbers in function without using ".reverse" method in Javascript

function revertNumbers(...numberArray) {
   let rev = [];
   for(let i = 0; i <numberArray.length; i++)
   {
     rev.push(numberArray[i])
   }
   return rev.reverse();
}

console.log("revertNumbers", revertNumbers(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) === "9,8,7,6,5,4,3,2,1,0");

Can you please show me the how to reverse number in this code that the statement will be true? Also without using .reverse method. Is it possible to make it in another for loop by just changing this statement:

(let i = 0; i <numberArray.length; i++)

Upvotes: 1

Views: 119

Answers (6)

Kosh
Kosh

Reputation: 18393

You might reverse like this:

function reverse(...a) {
  const h = a.length >> 1, l = a.length-1;
  for (let i = 0; i < h; ++i) [a[i], a[l-i]] = [a[l-i], a[i]];
  return a;
}

console.log(reverse(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).join(','));

Upvotes: 0

Stephen M Irving
Stephen M Irving

Reputation: 1512

Here is a solution that manipulates the array in place, and only has to traverse half of the original array in order to reverse it. This ekes out some modest performance gains compared to other answers in this thread (my function narrowly beats out or matches the speed of even the native reverse method in ops/sec), but micro-optimizations are largely irrelevant for this problem unless you are talking about a truly massive list of numbers.

Nonetheless, here is my answer:

const revNums = (...numArray) => {
  for (
    let arrLen = numArray.length,
        breakPoint = ((arrLen / 2)|0) - 1,
        i = arrLen,
        k = 0,
        temp;
    --i !== breakPoint;
    ++k
  ) {
    temp = numArray[i];
    numArray[i] = numArray[k];
    numArray[k] = temp;
  }

  return numArray.join(',');
};

Upvotes: 1

Mister Jojo
Mister Jojo

Reputation: 22265

It is to ask how many solutions can be possible ?
here are 3 of them...

"use strict";

const targetString = '9,8,7,6,5,4,3,2,1,0'
  ;
function soluce_1(...numberArray)
  {
  const rev = [];
  for( let i=numberArray.length;i--;) { rev.push(numberArray[i])  }
  return rev.join(',')
  }
function soluce_2(...numberArray)
  {
  const rev = [];
  let   pos = numberArray.length;
  for(let N in numberArray) { rev[--pos] = N }
  return rev.join(",")
  }
function soluce_3(...numberArray)
  {
  const rev = [];
  while(numberArray.length) { rev.push(numberArray.pop()) }
  return rev.join(',')
  }


console.log('soluce_1 ->', (soluce_1(0,1,2,3,4,5,6,7,8,9)===targetString) );
console.log('soluce_2 ->', (soluce_2(0,1,2,3,4,5,6,7,8,9)===targetString) );
console.log('soluce_3 ->', (soluce_3(0,1,2,3,4,5,6,7,8,9)===targetString) );

And yes, I code in Whitesmiths style, please respect this (the reason for the downVote for correct answers ?)

https://en.wikipedia.org/wiki/Indentation_style#Whitesmiths_style

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

You could take a value and the rest of the arguments and use a recursive approach to get a reversed array of arguments.

function revertNumbers(v, ...rest) {
    return rest.length
        ? [...revertNumbers(...rest), v]
        : [v];
}

console.log(revertNumbers(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));

Upvotes: 0

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

You could reduce the original array and unshift each element onto the new array.

function revertNumbers(...numberArray) {
  return numberArray.reduce((r, e) => { r.unshift(e); return r }, []).join(',')
}

console.log("revertNumbers", revertNumbers(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) === "9,8,7,6,5,4,3,2,1,0")

If you don't want to unshift, you can concat in reverse.

function revertNumbers(...numberArray) {
  return numberArray.reduce((r, e, i, a) => r.concat(a[a.length - i - 1]), []).join(',')
}

console.log("revertNumbers", revertNumbers(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) === "9,8,7,6,5,4,3,2,1,0")

Upvotes: 0

Maheer Ali
Maheer Ali

Reputation: 36564

You just need to reverse the direction of your loop. Means start i with last index and then gradually decrease it to 0

function revertNumbers(...numberArray) {
   let rev = [];
   for(let i = numberArray.length - 1; i >= 0; i--)
   {
     rev.push(numberArray[i])
   }
   return rev.join(",")
}

console.log("revertNumbers", revertNumbers(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) === "9,8,7,6,5,4,3,2,1,0");

This is also a good use case of reduceRight()

const revertNumbers = (...arr) => arr.reduceRight((ac, a) => ([...ac, a]), []).join(',')

console.log("revertNumbers", revertNumbers(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) === "9,8,7,6,5,4,3,2,1,0");

Upvotes: 3

Related Questions