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

2
  • 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); Commented Dec 30, 2020 at 6: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. Commented Dec 30, 2020 at 6:40

7 Answers 7

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);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use reverse() and join()

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

Comments

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

Comments

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

Comments

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

Comments

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)

Comments

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

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.