5/6 Control Flow Know-How


#1

Would like some help with my code. It goes through as correct, but when I run it, it returns every word as REDACTED. So clearly my code isn't correct. I'm not sure what I'm doing wrong though.

puts "Please proceed with data entry"
text = gets.chomp

puts "What word is classified?"
redact = gets.chomp

words = text.split(" ")

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


#2

Hey @nycwebs212

You've got two smaller errors in your code.

The first one is that you got a single "=" for assigning the variable instead of the comparison "==".
The reason for the if-test passing, is that the redact variable is first assigned to the words variable, and then the if-test will check if words is equal to 'true'. Since every value besides 'nil' and 'false' will be read as true, the if-test is valid and he prints "REDACTED".

The second issue is that you use "words", the array variable instead of "word", the block variable.

Changing that line to "if word == redact" should make it execute as wanted


#3

Can you help me?Why my code is not running?
puts "Palavra a ser pesquisada:"
text = gets.chomp

puts "Palavra a ser editada:"
redact = gets.chomp

words = text.split(" ")
puts words
words.each do |word|
print word
if text == redact
print "REDACT"
else
print word + " "
end
end


#4

Hey Skillix

I'll be posting the modified code at the bottom, but explain first the changes that was made

First thing, is that I removed the "puts words" on line 8 and "print word" on line 10, since they were just printing out extra text.
The first one, "puts words" could have stayed, since it would print out everything and add a new line at the end, but the "print word" which happened for every word would interfere with the interpreter that would read the final result.

Secondly, you were comparing the wrong variable, using the full text version against the redacted word "if text == redact", instead of the individual word "if word == redact".

Thirdly, is that you needed to change 'print "REDACT"' with 'print "REDACTED "', since the example provided that text and the interpreter that checks if you failed or succeeded the challenge would be looking for that precise pattern in the result.

puts "Palavra a ser pesquisada:"
text = gets.chomp

puts "Palavra a ser editada:"
redact = gets.chomp

words = text.split(" ")

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