Sorting With Control Flow


#1



I am stuck with Ordering Your library exercise.


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?


#2

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


#3

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)


#4

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.


#5

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


#6

Yes, it works.
Thanks for help.


#7

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.


#8

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