Redacted 6/6 Challenges - multi redacted words


#1



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


Hey Guys and Girls,

I have completed the Redacted lesson and I want to go on a do the extra challenges.
I am working on a program that takes multiple separate words to redact.

Below is my code, but when I use it it prints "REDACTED" for all the words I input first.

Why is this?


puts "Give me a quote"
text = gets.chomp
 text.downcase!

puts "Take away one word"
redact = gets.chomp
 redact.downcase!

puts "Do you want to take away another? Yes or No"
ans = gets.chomp
 ans.downcase!
 if ans == "yes"
     puts "Ok, just one more then.."
     redact2 = gets.chomp
     redact2.downcase!
 else
     print "As you wish"
 end

words = text.split (" ")
redacts = [redact, redact2]

words.each do |word|
    if redacts.include? (word) == false
        print word + " "
    else
        print "REDACTED "
    end
end


#2

Consider the case when the user do not want another redact word. Then your redacts array: redacts=[redact,redact2] would be meaningless!


#3

Thanks for the reply!
Ah see your point.....

Is there a way I can use only one "redact" input and ask the user to input multiple words at the same time and get these words to be redacted from the final output....?

Many thanks


#4

Well, maybe.
For redaction you can ask the user to enter not a word, but a group of words. Than you can split these words to be redacted by split(" "). By this way you'll have an 'array of redaction words'.
If you use this way, you have to check the user text for each element of your array.


#5

I think I get it, I will get it a try.
Many thanks.


#6

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