Part 5, small bug: problem with arr.sort!


#1

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

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

end

numbers = [2,6,3,8,9,15,12,1,7]
alphabetize(numbers)

puts numbers

The program rejects the solution if the .sort method is called outside of the if statement. The error goes along the line of "Your program doesn't default to alphabetizing if there's no second argument"


Sorting With Control Flow - help Oops, try again. It looks like your method doesn't default to alphabetizing an array when it doesn't receive a second parameter
#2

I stumbled here a little bit ago myself but you need to re-read what they are doing in the prior step when they have you declare the initial method like

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

The behavior of the interpreter is to implicitly return 'arr'. When you stick the conditional statement in, 'arr' alone is no longer the final statement so it doesn't return 'arr'. There's two ways to deal with this, either use 'return arr' or just put 'arr' after the conditional.

def alphabetizer(arr, rev=false)
    arr.sort!
    if rev
        arr.reverse!
    end
    return arr #explicitly return 'arr'
end

Note: I used an explicit 'return arr' because you know what it is doing. The following does the same thing but highlights why not explicitly using return may be confusing:

def alphabetizer(arr, rev=false)
    arr.sort!
    if rev
        arr.reverse!
    end
    arr #implicitly returning 'arr' from the method
end

#3

Adding the "arr" or "return arr" before the end of the alphabetize method indeed gives me green pass on it.

But in my opinion, I don't think it's a "must-go" to return something on that method, since we change the original array with those "!"s. This alphabetize method can be like "welcome" method: does its job and no need to return anything.


#4

Is this requirement intentional?


#5

I do think so. There is no request to add "return". It made me confused a lot.


#6

Agreed with everything posted here. I knew how to resolve the issue but it is not discussed anywhere in the lesson and clearly has confused more than a few folks.


#7

Can anyone tell me what I did wrong?
code: def alphabetize(arr, rev = true )
arr.sort!
if rev == false
arr.reverse!
end
end
numbers = [1,2,3,4,5]
numbers.reverse!
puts numbers
codecademy says: It looks like your method doesn't default to alphabetizing an array when it doesn't receive a second parameter


#8

I have the exact code line by line as @webrockstar14925 . Hopefully someone can shed some light. Thanks.


#9

ok so I technically got this question correct/passed, however on the console the numbers are still in numerical order, not reverse order (?) now I'm just confused

here's my coding

def alphabetize(arr, rev=false)
arr.sort! { |item1, item2| item1 <=> item2 }
if rev==true
arr.reverse!
else
arr
end
end

numbers = [1, 7, 3, 9]

alphabetize(numbers)
puts numbers

Super confused!!


#10

Well explained, thank you a lot !


#11
if rev==true

This is the line taking care about reversing the array or not. Based on the fact that rev is initialized at false by default (def alphabetize(arr, rev=false)), what do you think will happen if you call alphabetize with only one argument?


#12

My first guess:

If the programm doesn't know the rev variable, he won't be able to read rev == true either.


#13

You are VERY close to the correct answer but you are just missing one extra object on your else statement. See my example that is working below:

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

numbers = [5, 1, 3, 6]

puts alphabetize(numbers)


#14

@hopisaurus

I'm sorry but your code won't work. If I call your method with: alphabetize(numbers, true), you won't sort the array in reverse, you'll just reverse it (feel free to try it out).

Actually @elesskay has a code that works correctly, it's just that he didn't realize how to use his method in reverse mode yet.


#15

I tried to simplify it, as much as possible per directions, and got this to pass:

def alphabetize(arr, rev=false)
    arr.sort!
    if rev
        return arr.reverse!
    else
        return arr
    end
end
numbers = [5, 1, 3, 8]
alphabetize(numbers)
puts numbers

I think the language used in one of the steps about returning the array made it confusing, though.


#16

Everything in this confused me, I don't know why returning arr works, maybe I missed something, but I understand it so I guess anybody who sees this should return arr between the two ends.


#17

def alphabetize(arr, rev=false)
arr.sort!
if rev
arr.reverse!
else return arr
end
end
numbers = [7, 19, 1, 13, 15]
alphabetize(numbers, rev=true)
puts numbers


#18

Step 4 of this exercise adds, "arr.sort!", which returns the sorted array.
Step 5 tells you to add the "if" statement, if rev == true, and stops there.
The program still needs to know what to do - if rev == false - as in the default parameters. So without an "else" statement you get "Oops, try again. It looks like your method doesn't default to alphabetizing an array when it doesn't receive a second parameter."
So the program will work after adding the "else" statement, and
can be done without needing to "return" arr again.

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

to see the numbers reversed, change the last line to:

puts alphabetize(numbers, true)

#19

there is what worked for me:

if rev != true // this tell the console, if rev is not true sort....
arr.sort
else arr.reverse! // else do reverse

hope that helps.


#20

I see a lot of folks maintaining arr.sort! in front of their if statements. That means it will execute that first which is what is causing the problem. If you move that to your else statement that will fix it.

def alphabetize(arr, rev=false)
    if rev == true
        arr.reverse!
    else
        arr.sort!
    end
end
arr = [2, 3, 1]
puts alphabetize(arr)