Lesson 4/6 fix for common mistake


#1

Hey there folks, if you are getting this error:

"Oops, try again. It looks like you didn't print each word from the user's text to the console."

When you start going through the array you made with you string you cannot give an output with a " " attached to it, You either need to have you space break as "" or none at all. There is nothing syntactically wrong with your code, the way they check code correctness for this project is off. Here is an example of code that would work in the Codecademy editor.

puts "Text to search through: "
text = gets.chomp
puts "Word to redact: "
redact = gets.chomp

words = text.split(" ")

words.each do |word|
  if word != redact
    print word + "" # <- These quotes must have no spaces. 
  else
    print "REDACTED "
  end
end

#2

Saved me a massive headache dude, thanks!


#3

Just to note that no where it is mentioned on lesson 4 that we have to add a space while printing each word, so it makes no sense to add a space. But many users are still doing this it seems so thank you @johnsprunger for writing this post :slightly_smiling: Additionally, please change the title to "..common mistake" as current title is misleading (there is no "bug" with the exercise itself) :slightly_smiling:

Thank you!

EDIT: I fixed the title myself.


#4

Try this simple code out,If still facing problems!!!
words = ['s', 'h', 'a', 'f', 'a', 't',]
words.each do |words|
print words
end


#5

puts "text to search through"
text = gets.chomp
puts "Words to redact"
redact = gets.chomp

words = text.split (" ")

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

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!).

is the mistake I keep getting, what has done wrong with the code or have I left something out?


#6

Did have the space between REDACTED and " got it to work!


#7

why doesn't it work the other way round?
my code is like this
i wanted to swap out the != redact

puts "Gib einen Text ein: "
text = gets.chomp
puts "Text to redact: "
redact = gets.chomp

words = text.split(" ")

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


#8

it is mentioned:
2. Otherwise (else), print word + " ".
the space is in the lesson as example on the left side


#9

ok it was the = that has to be a == at if


#10

Thank you so much for posting this!

The first time I saved and submitted my code I actually hadn't added the (+ " ") modification and still got the error, so be aware that this error checking the problem is not isolated to the space not being present. I only added the (+ " ") after checking the example code after getting the proper output printed to the console and still gettin an error.


#11

i thought i was losing my mind, thank you.