Rrf F
Rrf F

Reputation: 1

How to reverse the array with the function reverseArray

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

Upvotes: 0

Views: 267

Answers (7)

user9706
user9706

Reputation:

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 ? " " : ""));
    }
}

Upvotes: 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)

Upvotes: 0

D. Seah
D. Seah

Reputation: 4592

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);

Upvotes: 0

wxsm
wxsm

Reputation: 586

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]);

Upvotes: 0

Kevin Zhang
Kevin Zhang

Reputation: 1072

  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]);

Upvotes: 0

54ka
54ka

Reputation: 3589

You can use reverse() and join()

var arr = [1, 2, 3, 4, 5]; 
arr.reverse(); 
console.log(arr.join(' '));

Upvotes: 1

cbalakus
cbalakus

Reputation: 630

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);

Upvotes: 1

Related Questions