What is wrong with my code?


#1



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


I'm not sure what's going on, I've tried this about twenty times and it keeps rejecting my code.

Error message: Oops, try again.
Make sure to print each word from the user's text to the console
unless that word is the word to be redacted; if it is, print REDACTED
(all caps!).


puts "Enter text"
text = gets.chomp
puts "Enter text again"
redact = gets.chomp
words = text.split (" ")
words.each do |word|
print word
  if word == redact
    print "REDACTED "
  else
    print word + " "
  end
end


#2

Code looks fine. What happens if you inverse the condition

if word != redact

#3

I inversed it and still no luck. Got this message:

Oops, try again.
Make sure to print each word from the user's text to the console
unless that word is the word to be redacted; if it is, print REDACTED
(all caps!).

I don't know what could be wrong. I'm having a bit of trouble following or understanding this.

Code:

puts "Enter text"
text = gets.chomp
puts "Enter text again"
redact = gets.chomp
words = text.split (" ")
words.each do |word|
print word
  if word != redact
    print word + " "
  else
    print "REDACTED "
  end
end

#4

try to do this:

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


#5

Okay thanks, that worked. I am having trouble understanding this but we will see if it gets better.


#6

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