5 What is wrong here


#1
puts "Please enter some text"

text = gets.chomp
puts "Please enter a word to censor"
redact = gets.chomp

words = text.split(" ")

words.each do |word|
print word
if word == redact
print "REDACTED "
else
print word + " "
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!).

#2

You're printing your word regardless of the result of your if statement. get rid of this extra print and you should be fine.


#3

so can someone explain why two of the instance of end is needed here?


#4

@arjofocolovi

wich print are you talking about? i got the same code, still not understanding what is wrong with the code above


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

This is the inside of the each.

As you can see, it prints the word first. THEN it goes into an if/else statement and either print the word or REDACTED. So if it's a word to redact, the code will first print the word, and then print REDACTED, and if it's not a word to redact, the code will print the word twice.

That's not what we want, we want either the word or REDACTED to be printed.


#7

Please let people find answers by themselves, they won't learn anything by simply being given what to type. Coders are not secretaries, they're thinkers.


#8

@arjofocolovi

sometimes an answer is needed, with an explanation. as long they get the concept, or see what they are doing wrong. If under those conditions I don't see an issue giving the answer out.


#9

When it's a typo or a syntax error, yeah small things like this can be straight out given. When it's a problem of logic or thinking, it's never a good idea to give out code in my opinion.

If someone doesn't seem to understand a concept, then explain the concept, but you don't need to give out the answer to do that.

Of course it's tough, but learning is always tough. If it's not, it means it's not really learning ^^.


#10

Have you solved your issue by the way?


#11

yes i got through it. Yeah thats what i meant. like santx and mis spellings.