Lesson 5 code error


def alphabetize(arr, rev=false)
if rev == true
puts arr.reverse!
numbers = [1,3,5,2,6,4]

alphabetize(numbers, true)

This is the code I am running. The output is correct in the display, and my numbers are reverse. However, it won't let me go onto the next lesson because it says " Oops, try again. It looks like your method doensn't reverse-alphabetize an array when it receives 'true' as a parameter." Is there something I am missing? I looked at other discussions and the code seems to match.

Please help! Thanks


The submission test requires you to return the array.


Can you elaborate please?


Not much elaborating to do.

It requires your function to return the array, and yours does not.


Can you give an example of how this would look because it does return the array in the correct order? Even when I use return instead of puts it will not work


But you're probably not getting the same error. Sometimes there is more than one thing that needs changing. Read the next error.


Well, it "technically" works but still gives an error.

If I put return for both puts it says "Oops, try again. It looks like you didn't print anything to the console. Does your method use 'puts' to print the sorted array, and did you remember to call your method?" and if I put return just in front of either arr.sort! or arr.reverse! it gives the originial error.


Should the function print anything though?

Call the function, get the array, print the result.


Well, it prints what it supposed to. the array in reverse order but says it doesnt?


Are you puts-ing it?

The function should return an array and your code should then puts that returned value.
Gotta do these things in a very literal way, there's no such thing as close enough here. It's right or its wrong.


I had the same error and modifying my code to explicitly return the array solved my problem. I also modified my output to use the returned value (see below). I think that the instructions need to be reviewed here. In the previous exercise I didn't need to do this and the instructions do not specify using return.

 def alphabetize(arr,rev=false)
    if rev == true
    return arr

numbers = [7,2,5,3,1]

result = alphabetize(numbers)

puts result