5: Refusal to reverse-alphabetize


#1

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


I think that the rev is not working with my code, apparently...
ERROR:
Oops, try again. It looks like your method doensn't reverse-alphabetize an array when it receives 'true' as a parameter.
(also, typo.)

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

numbers = [653, 5692490, 98764, 18, 627654, 5, 7924]

puts "#{alphabetize(numbers)}"
puts "#{alphabetize(numbers, true)}"


What's wrong?


#2

puts "#{alphabetize(numbers)}"
puts "#{alphabetize(numbers, true)}"

Should be

puts something(something, something)

and that should do the trick.


#3

That did not work for me. I still got the "doesn't reverse-alphabetize" message.


#4

The problem has been solved.
My old code:

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

My new code:

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

Must have been something to do with the "return arr.sort!" bit.


#5

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