What's wrong with my code?


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-nOho7/0/5?curriculum_id=5059f8619189a5000201fbcb


I get the right result, but it won't allow me to continue for some reason. I get this error message: "Oops, try again. It looks like your method doesn't default to alphabetizing an array when it doesn't receive a second parameter." No idea what that's supposed to mean, because it does sort properly whether I want the array sorted normally or in reverse.


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

def alphabetize(arr, rev=false)
    arr.sort!
    if rev 
        arr.reverse!
    end
end

alphabetize(numbers, rev=true)
puts numbers


#2

Never mind, I figured it out. But it doesn't make any sense! My first code printed the right results to the console, so why did they make me do this?!

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

def alphabetize(arr, rev=false)
arr.sort!
if rev
arr.reverse!
else
arr.sort!
end
end

alphabetize(numbers)
puts numbers

Why does it make me repeat a bit of code I already did, if leaving it out still gets the job done?!


#3

It's looking at the return value, not particularly clear from instructions, error message or function behaviour.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.