Lesson 5 code error


#1

def alphabetize(arr, rev=false)
arr.sort!
if rev == true
puts arr.reverse!
else
arr.sort!
end
end
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


#2

The submission test requires you to return the array.


#3

Can you elaborate please?


#4

Not much elaborating to do.

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


#5

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


#6

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


#7

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.


#8

Should the function print anything though?

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


#9

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


#10

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.


#11

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)
    arr.sort!
    if rev == true
        arr.reverse!
    end
    return arr
end

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

result = alphabetize(numbers)

puts result