Redacting words with period at the end


#1



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


I want my code to be able to redact words at the end of a sentence if there's a punctuation mark at the end so I added an OR condition.

But what ends up happening is it redacts the entire sentence with "REDACT REDACT REDACT" for every word in the sentence.

I don't get any error messages.

Can someone please help me figure out where I went wrong?


puts "Write something: "
text = gets.chomp.downcase
puts "This is what you want to redact: "
redact = gets.chomp.downcase

words = text.split(" ") 

words.each do |words|
if words == (redact + ".") || redact
    print "REDACTED "
else 
    print words + " "
end    
end


#2

I think you would take the same code (just words == redact) and add an extra delimiter, so instead of:

words = text.split(" ")

It would be:

words = text.split(/[\s,.]/)

I think that should work.


#3

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