Part 5, Passes but not with correct intention

Hello magnificent human beings!!!

If you are here and able to clarify or help me solve this problem you will be the hero of this glorious Wednesday!

As stated in the the title, my code passes the test but does not perform the desired function! I have altered it a few times which still yields a pass but again not the functionality.

def alphabetize(arr, rev=false)
    arr.sort! puts numbers
    if rev==true
        arr.reverse!
end
end
numbers = [ 0, 1, 2, 4, 6,10]

So in the instances where I have altered the code in an attempt to get the functionality I have altered the methods argument to make [rev=true].

My reasoning behind this is that if the method argument states it’s true and then I have an [if] statement saying DO THIS IF IT IS TRUE ; it should perform the desired task. Which it of course doesn’t. So Lovely.

So I’ve spent a few moments altering it, as well as putting the fear of god into the syntax by highlighting it and holding my finger centimeters off the delete key for denying me the functionality I’m looking for all the while being reluctant to reverting back to part 1 of the lesson and simply copying the if/else/elsif format there; without luck.

I will be moving on from this task but would love clarification from You about how to fix this functionality error!

~Cheers!

3 Likes

I had a similar issue too. My code would pass but I didn’t get a descending order of my values when I set rev=true. I just got the reverse of my values in order that they appeared in the array i.e. [1,6,5,3] would change to [3,5,6,1] rather than the desired [6,5,3,1]. So what I had to do was sort the array first then add an if/else statement to account for both conditions (rev=true, rev=false). This probably isn’t the most elegant way to do it as the double “arr.sort!” seems redudant, but it worked and that’s what’s important.

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

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

Merci beaucoup, ça faisait un moment que j’étais bloqué. =)

Thank you very much , it was a moment That I was stuck. =)

I don’t know if this lesson got debugged but i post my code which actually has the functionality.

def alphabetize(arr, rev=false)
arr.sort!
if rev == true
arr.reverse!
else
arr.sort!
end
end
numbers = [1, 15, 11, 4, 24]

alphabetize(numbers, true)
puts numbers