Tryin to do the challenge....Wont redact the other words


#1


puts "Enter your text: "
text = gets.chomp`
puts "Declare the redact: "
redact = gets.chomp
words = text.split(" ") 

words.each do |d|
    if d == redact || d.upcase == redact || d.downcase == redact || d.size == redact.size
        print "REDACTED "
    else
        print d + " "
    end
end
<do not remove the three backticks below>

Replace this line with your code.


#2

The above line has an invalid token (the back-tick at the end) that should be removed.

What could you do to make sure your redactor redacts a word regardless of whether it's upper case or lower case?

This brings about another question: is redact always lowercase? We would need to set this condition up front. What if it is a proper name that we wish to redact, then it will be capitalized, and neither upper nor lower case. However, since it is going to be redacted and not in the return string, it should be fair to suggest we force redact to lowercase off the top.

The same could be said of the word, itself. IF it is to be redacted. We don't want to change the state of words that are to remain in the returned string. So this means case change in the condtional, and not the actual text. This seems like a workable scenario, right?

puts "Enter your text: "
text = gets.chomp

This text will preserve state on all unredacted words.

puts "Enter a word to redact: "
redact = gets.chomp
if redact
    redact = redact.downcase
    words = text.split(' ')
    # code
else
    print "Nothing to redact."
end

Now the stage is set. The following code will be inserted where indicated.

words.each do |word|
    if word.lower != redact
        print word + " "
    else
        print "REDACTED "
    end
end

Note that we have not changed the state of the word, only created a transient value in lowercase.

That covers this point. There are still other challenge points to go over. Let's see how you make out with them.


#3

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