0

I am attempting to create a function that can take in an array and display its contents backwards. I am having trouble with understanding why my function call is showing undefined when I enter an array in its parameter.

var arrayOne = []

function printReverse(arrayOne) {
    for(var i = arrayOne.length-1; i < 0; i--) {
        console.log(arrayOne[i])
    }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
brownmamba
  • 13
  • 2
  • 8
  • 5
    Your function doesn't contain a `return` statement, so it doesn't return a value. – melpomene Jun 20 '19 at 20:33
  • 2
    how are you calling your function? – Taylor A. Leach Jun 20 '19 at 20:34
  • @melopomene After swapping out the console.log() with return I'm having the same problem... how would you suggest I incorporate return into this function? – brownmamba Jun 20 '19 at 20:35
  • @TaylorA.Leach I've tried both printReverse(1,2,3) and printReverse([1,2,3]) – brownmamba Jun 20 '19 at 20:36
  • 1
    Possible duplicate of [Reverse array in Javascript without mutating original array](https://stackoverflow.com/questions/30610523/reverse-array-in-javascript-without-mutating-original-array) – Web Nexus Jun 20 '19 at 20:36
  • If your array is not empty your loop is never entered, because `i < 0` is false. If your array is empty it doesn't mean anything to print the contents backwards. – Paul Jun 20 '19 at 20:37
  • @user10463856 Well, what's the value that you want to return? – melpomene Jun 20 '19 at 20:37
  • Do you want to *display* the contents of the array in reverse order, or do you want to create a new array with the contents reversed and return that? – Bergi Jun 20 '19 at 20:41
  • 1
    @Bergi I wanted to display the contents of the array in reverse order, but I have it figured out now. Thanks – brownmamba Jun 20 '19 at 20:43
  • 1
    @user10463856 Ah. Looks like everyone was confused because you used the term "return". – Bergi Jun 20 '19 at 20:45

3 Answers3

1

There is a misunderstading with your question:

What you want to achieve is console.log elements on screen, not return anything.

Your code

var arrayOne = []

function printReverse(arrayOne) {
    for(var i = arrayOne.length-1; i < 0; i--) {
        console.log(arrayOne[i])
    }
}

Does not work because you have a wrong operator in your code at i < 0. This will return false at first iteration, because i will be arrayOne.length, which would be > 0 if there is any element on it.

Change this part to i >= 0 and your code will work and actually print the values on console.

However, if you really want to have a reverted array, then you should simply use Array reverse() instead of writing a function to return it.

Renan Souza
  • 905
  • 9
  • 25
  • Yep, it works now, I'm new to JS and programming in general so I overlooked a fundamental component of a for loop. Thank you for your time – brownmamba Jun 20 '19 at 20:41
0

so there are some fundamentals that are off there. as stated in another answer i will never be less than 0 because you are defining it as a value greater than 0 in your for loop. Give something like this a try

EDIT: the comments are correct in the sense that the array will be mutated so make a copy of the array first which I've added using the spread operator Also as far as this returning undefined -- it should return undefined unless you comment out the return statement

const arrayOne = [];

function printReverse(array) {
    if (!Array.isArray(array) && array.length === 0 ) {
        return 'The array is empty';
    }

    const arrCopy = [...array];

    // technically you could just reverse it
    // if you return it you have to assign it to someone on the function call

    // return arrCopy.reverse();

    // if you want to log the reversed array you could also
    // console.log(arrCopy.reverse());

    // if you want to reverse it then log each single index
    // arrCopy.reverse().forEach(function(item) {
    //     console.log(item);
    // })
}

// if you were to just return the reversed array you would have to assign it to a variable
// this is just an example and wouldnt technically work because arrayOne is empty
// also if you use this YOU HAVE TO RETURN THE ARRAY COPY
// const reversedArray = printReverse(arrayOne);
  • printReverse(arrayOne) will actually mutate arrayOne, also, reversedArray will be undefined unless the array is empty. Your function is not returning anything :/ – Renan Souza Jun 20 '19 at 20:50
  • Yep I figured the i<0 was the main flaw, the forEach excerpt is def cleaner than what I had. Thank you for your time – brownmamba Jun 20 '19 at 20:50
  • I edited the post. You are correct @Renan that this will mutate the array so I updated the post to make a copy of the array. You will get undefined if you do `const reversedArray = printReverse(arrayOne);` if you don't uncomment the return statement. It doesnt return undefined for me – Michael Cacciano Jun 20 '19 at 20:58
-1

If you want it to return something, you have to add a return within the function, such that

function printReverse(arrayOne) {
    for(var i = arrayOne.length-1; i < 0; i--) {
        console.log(arrayOne[i]);
    }
    return "";
}

However, in your case, this doesn't make a lot of sense. You can only return one thing, be it a String, int, array, object, whatever. But once your program hits one return statement, it will quit the function after returning the value.

isherwood
  • 58,414
  • 16
  • 114
  • 157
HunterBerry
  • 165
  • 11
  • Apologies for the miscommunication, intention was to console.log each value separately as opposed to returning it per se, can see how that caused confusion. Have it figured out now, thank you for your time – brownmamba Jun 20 '19 at 20:52