5. It looks like your method doesn't default to alphabetizing


#1



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


The code throws an error: Oops, try again. It looks like your method doesn\t default to alphabetizing an array when it doesn't receive a second parameter.


def alphabetize(arr, rev=false) #is this not what the error is supposed to rep? the same code worked in the last #exercise!
    arr.sort!
    if rev==true
        arr.reverse!
end
end

numbers= [5,4,3,65,3,5,3]

puts alphabetize(numbers)
puts alphabetize(numbers, true)


#2

Compare your output to what you wrote.
You call your method twice and print the results each time. Those numbers should therefore be printed twice. Are they?


#3

No they are not. I only added the second call to see if the outputs differ. For some reason the code fails after I go from:

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

to

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

Probably should edit+test the code in local interpreter instead of the codeacademy one. Then I could remove one failure point.


#4

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

numbers= [5,4,3,65,3,5,3]


puts alphabetize(numbers, true)

This solution works. Looks a bit crude and I am not happy with it but it's good enough.


#5

I'm having the same issue with this one. Did what it asked, but getting the error. Might just have to use your solution to get through to the next question!

Btw, not to be anal, but your indentation could use some work :wink:


#6

Ah, I changed it so that the arr.sort is in the if else statement instead of above and it accepted that. Seems like the instructions could use a little rewording.

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

#7

I am not entirely sure how the program flow handles it but it seems to me, intuitively, that if you get an unsorted array then in your version if
rev==true
then your flow would just skip the
arr.sort!
and you'll end up reversing an unsorted array which is why in my solution I included one before the condition check as well.


#8

Yep, tried it out and you're correct... It did accept it though, and that's all that matters, right? :wink:

Thanks for putting me straight!


#9

I realized that it would be better to work on a variable cause that would skip us sorting the array twice. Save us some cycles. :slight_smile:

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

numbers= [5,4,3,65,3,5,3]

#10

I still don't understand why this does not work:

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

numbers = [78,20,50,234,42,6,18,98,39,19,25]
puts alphabetize(numbers)

It seems to me that the array will always get sorted in ascending order first, and then, if rev is true, it will reverse the array. But when I run the code, nothing gets output. If I delete the if statement, it outputs the ordered array just fine.


#11

you must use 'return' for it to work.

your solution will work if you put

return arr

before the end of the method. I got stuck on this for a bit, because without a return the code will run correctly and give you the right output.


#12

But shouldn't arr be returned on its own since,its the last line of code as mentioned before in codecademy? ( since, the if block is not running thus, arr.sort! is the default return to the method like in the example given in **1)What you'll be building section **
My code works ,when I complete the "if ,else block of code" as shown below:

def alphabetize(arr , rev=false)

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

end
numbers= [3,6,-4,9,10,21]
puts alphabetize( numbers )

It may be that the default "rev value of 'false' isn't working" ?:


#13