0

You are given an array with N length. Please develop the function reverseArray to reverse the array

E.g. Input: [1, 2, 3, 4, 5] Output: 5 4 3 2 1


My attempt is as follows:

function printArray (inputArray){
    for(let i = 0; i < inputArray.length; i++){
        console.log(inputArray[i]);
    } 
}

function reverseArray (inputArray){
 
    for (var i = inputArray.length - 1; i >= 0; i--){
       inputArray.push(inputArray[i]);
    }
    
    printArray(inputArray);

}

reverseArray([1, 2, 3, 4, 5]);

But it turns out to be as follows: 1 2 3 4 5 5 4 3 2 1

Can anyone teach me how to solve, i have been struggling with this question for 2 days

Rrf F
  • 1
  • 1
    Use a new array to push data. ``inputArray`` already has 1 to 5 thus you are getting repetitive values. Inside ``reverseArray`` function use something like this, ``let newArr = [];`` and then inside for loop use ``newArr.push(inputArray[i]);`` and then ``printArray(newArr);`` – Not A Bot Dec 30 '20 at 06:05
  • The problem statement is ambiguous. Does "reverseArray to reverse the array" mean the array have to reversed in-place or that you want a new array? Also the name applies that you want an array but the output you give is a string. – Allan Wind Dec 30 '20 at 06:40

7 Answers7

1

Check this link

function reverseArr(input) {
    var ret = new Array;
    for(var i = input.length-1; i >= 0; i--) {
        ret.push(input[i]);
    }
    return ret;
}

var a = [3,5,7,8]
var b = reverseArr(a);
cbalakus
  • 620
  • 7
  • 15
1

You can use reverse() and join()

var arr = [1, 2, 3, 4, 5]; 
arr.reverse(); 
console.log(arr.join(' '));
54ka
  • 3,501
  • 2
  • 9
  • 24
0

You could iterate through half the array and swap [i] and [inputArray.length-1-i].

function reverseArray(a) {
    for(let i = 0; i < Math.floor(a.length / 2); i++) {
        [a[i], a[a.length - 1 - i]] = [a[a.length - 1 - i], a[i]];
    }
}

If the output is really a string (opposed to an array) just print the elements start from the end:

function reverseArray(a) {   
    for(let i = a.length - 1; i >= 0; i--) {
        process.stdout.write(a[i].toString()  + (i ? " " : ""));
    }
}
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
0
  1. Use for loop:

function printArray (inputArray){
    for(let i = 0; i < inputArray.length; i++){
        console.log(inputArray[i]);
    } 
}

function reverseArray (inputArray){
    var results = [];
    for (var i = inputArray.length - 1; i >= 0; i--){
       results.push(inputArray[i]);
    }
    
    printArray(results);

}

reverseArray([1, 2, 3, 4, 5]);
  1. User array's reverse method:

    function printArray (inputArray){
        for(let i = 0; i < inputArray.length; i++){
            console.log(inputArray[i]);
        } 
    }

    function reverseArray (inputArray){
        var results = [];
        results = inputArray.reverse();
        
        printArray(results);

    }

    reverseArray([1, 2, 3, 4, 5]);
Kevin Zhang
  • 1,012
  • 6
  • 14
0

A bit change on your base code. It could be simpler, but I think what you need is a inplace reverse.

function printArray(inputArray) {
  for (let i = 0; i < inputArray.length; i++) {
    console.log(inputArray[i]);
  }
}

function reverseArray(inputArray) {
  const tempArr = inputArray.slice();
  inputArray.splice(0, inputArray.length);
  for (var i = tempArr.length - 1; i >= 0; i--) {
    inputArray.push(tempArr[i]);
  }

  printArray(inputArray);
}

reverseArray([1, 2, 3, 4, 5]);
wxsm
  • 556
  • 4
  • 14
0

there are many ways to do this. and there is a reverse method for array too.

function reverseArray(inputArray) {
  let start = 0;
  let end = inputArray.length -1;

  while (start < end) {
    const tmp = inputArray[start];
    inputArray[start] = inputArray[end];
    inputArray[end] = tmp;
    start++;
    end--;
  }
}

arr = [1, 2, 3, 4, 5];
reverseArray(arr);
console.log(arr);
D. Seah
  • 4,472
  • 1
  • 12
  • 20
0

thats very simple you can use javascript method "reversed()"

or you can use this function

function reverse(input) {
    var ret = new Array;
    for(var i = input.length-1; i >= 0; i--) {
        ret.push(input[i]);
    }
    return ret;
}

var arr = [3,5,7,8]
var b = reverse(arr)

console.log(b)