Control Flow Know-How


#1


Why do I get this correctly, when if should technically replace Alfa with redacted?


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


Replace this line with your code.


#2

There must be enough required elements in the submission to satisfy the lesson checker, but this code should definitely not have passed. No matter. You at least recognize that there is a problem and stuck around long enough to find out more. That's a crucial aspect of self-learning.

Foregoing any inputs that may be required, we can use your constants since we know that the lesson will pass without user input. You start with an array, which is fine. User input would be split to form one so that step is taken. Perhaps store your chosen word in a variable so it is not a part of the code.

redact = "alfa"

We cannot compare an array to a string, so,

if words == 'alfa'

will never be true.

There seems to be some missing code, here. The each statement.

words.each do |word|

end

Now inside the loop body we would add the if statement.

    if word == redact
        print "REDACTED "    # remember to include the space character
    else
        print word + " "
    end

Note the difference between the array name and the block parameter:

words => array
word  => block parameter

#3

Thanks a lot, will go back to that block and apply it!


#4

Actually, I still don't pass. I am a bit confused about the structure of where one should place the isolated variable and word; does that matter? (in my case redact = "alfa")
When you say inside the loop body, I assume it is the way I have structured it?

I get the following error message: (ruby):8: syntax error, unexpected $end, expecting keyword_end


#5

You have left off the end keyword for each.

... each do 
    if ...
    else
    end
end

#6

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