Sorting with control flow


#1

I keep getting stuck at the last exercise and I'm not sure what the issue is. It's saying that my method doesn't default to alphabetizing an array but it did what it's supposed to do in the console.

def alphabetize(arr, rev=false)
arr.sort!
if rev==true
arr.reverse!
end
end
numbers=[2,7,4,8]
alphabetize(numbers, rev=true)
puts numbers

the console prints out vertically: 8,7,4,2 nil


#2

you should put a else statement like that

 def alphabetize(arr, rev=false)
        arr.sort!
        if rev == true
            arr.reverse!
        else
            arr.sort!
        end
    end
    numbers = [1, 8, 9, 5, 10, 6]

    puts alphabetize(numbers)
    puts numbers

#3

thank you! I have anxiety around asking questions on forums but this encouraged me to continue asking questions!


#6

I dont konw how to flag, can you show me how to do it ?


#7

Not having any idea, at the top of the page under 2. I clicked the link labeled, 'Check out this forum thread on how things work over here. ', scrolled down about 3/4s through the page to a title, 'What else can I do'. It looks like a flag button should appear. I don't see it though, if you don't either maybe the only person who sees it is the person who began the thread - morgan2016 in this case.


#8

Yes, only morgan2016 can do it


#9

what am i flagging? it says that bgmart withdrew their posts


#10

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

end
numbers = [1,4,2,3,5]
puts alphabetize(numbers)


#11

Can anyone explain what is happening here :

def alphabetize(arr, rev=false)
arr.sort!
if rev == true
arr.reverse!
else
arr.sort!
end
end
numbers = [1, 8, 9, 5, 10, 6]

puts alphabetize(numbers)
puts numbers

If arr was not set to true or false, how did I end up with a standard out, print of 8,1,3,5,8, nil?