Figure out what is wrong with my code


#1

Can someone help me figure out what is wrong with my code, I keep getting the error code that I need to print every word and print redact. Here is my code:
puts "Write a sentence"
text = gets.chomp
puts "Give a word to redact"
redact = gets.chomp

words = text.split(" ")

words.each do |word|
print word
end

if word == redact
print "REDACTED "
else
print word + " "
end


Redacted 5/6
Redacted 5/6
#2

Hey there, hopefully you've figured it out by now. If you haven't, and aren't currently discouraged, I can try my best to help you out!

So for this lesson, if I'm not mistaken, you're taking in the string (sentence of however many words) as well as the word you want to remove from the sentence (redact).

Your first 5 lines of code (technically 6 lines with your spacing) are correct. After that is where you start to stray a bit. Your code currently (after line 6) prints out every word stored in the array words. This is already incorrect from what you want, considering the user isn't interested in all the words, but just the sentence with the redacted word out of it. So just a list of what you have that is incorrect:

  1. You print out each word that is in the sentence
  2. You only check if the 1 word is equivalent to redact, not even sure what that word ends up being since it's an array and I didn't run your code myself

You want to perform the words.each loop the way you did, and then inside that loop check if word == redact like you did. Both of your loops are correctly written, you just need to intermingle the two to get your desired outcome (the string without the redacted word).

If you still can't figure it out, I've posted my code below for you to look at, copy, paste, try, etc. Let me know if this solves your issue, and if not, I'll gladly help!

    puts "Enter text:"
    text = gets.chomp
    puts "Pick what you want redacted from the above text:"
    redact = gets.chomp
    words = text.split(" ")
    words.each do |word|
      if word == redact
        print "REDACTED "
      else
        print word + " "
      end
    end

#5

#6

THANK YOU SO MUCH FOR YOUR POST!!!
I was losing my mind!