Sorting With Control Flow

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
I am stuck with Ordering Your library exercise.

<In what way does your code behave incorrectly? Include ALL error messages.>

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

numbers = [5, 1, 3, 8]
puts alphabetize(numbers)
```

I got an error massage. Where I am wrong?

<do not remove the three backticks above>

@stanisa,
What is the exact Oops- or Error-message
and
why are you sorting the Array BEFORE the IF ELSE statement…

I have the same problem I fixed it like this

def alphabetize(arr,rev=false)

if rev
    arr.reverse!
else
     arr.sort!
end

end

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

Because the lesson instructions say “After your .sort! call, add an if statement.” (Emphasis added.) The lesson seems to be broken, insofar as you can only get it to pass by using an inefficient structure.

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

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

puts "#{alphabetize(numbers)}"

this seems to work. ```
type or paste code here

Yes, it works.
Thanks for help.

yes the instructions are quiet misleading.

  1. we need to accept array.
  2. if rev is false, then we should not REVERSE it. just sort it in ascending order
  3. if rev is true then REVERSE the array with reverse! method.
    i was getting correct output but also with error generated.
1 Like

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