Control Flow Know-How


#1

Good! There were two problems with our output, though: we didn't have spaces between our words, and our program didn't actually replace the word we wanted to redact with the word "REDACTED".

if var == 10
print "Variable is 10"
else
print "Variable is something else"
end
We can fix that with some if/else magic! The above example just

My Code is:

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 "REDACTED"
else
print word + " "
end
end
WHAT did i do wrong keep on saying

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

There's a problem here.


#3

which is ??
:stuck_out_tongue_closed_eyes:


#4
if var == 10

There's no problem here.


#5

You have if word = redact

It needs to be if word == redact

Your declaring a new variable by using one = and that throws off the program. == means you asking if these two items equal each other which is what you want.


#6

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!). (it says that every time i submit the code


#7

make sure there is a space after the word "REDACTED " and the last quotation marks.

"REDACTED "


#8

puts "Digite el texto a buscar:"
text = gets.chomp
puts "Digite la cadena:"
redact= gets.chomp
words = text.split(" ")

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

end


#9

the 'if text == word' should be 'if text == redact' or 'if word == redact'


#10

puts "duuuuude"
text=gets.chomp
puts "poooop"
redact=gets.chomp
words="boobs".split(",")
words.each do |word|
if word == redact

print "REDACTED"

else
print word + " "
end
end

Can someone explain to me why I need to put two ends please.


#11

same question-- I was having a ■■■■ of a time figuring out what was wrong, but then I put a second end, and it worked perfectly. Someone explain please, or did you figure out why?

puts "Please enter your innermost thoughts here: "
text = gets.chomp
puts "Word to be redacted: "
redact = gets.chomp

word = text.split (" ")

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


#12

It's pretty simple but you have to pay attention. Just as a reminder, and to make you think a little :wink:

The "end" actually ends any "function" you are telling the computer to do, so look for how many functions you are running in that block of code and you will see why you needed 2 "end"s.

Hope it makes sense and it helps anyone!


#13

You need one 'end' for the .each function, and another end for the if/else loop


#15

Something is wrong with this exercise. As you can see from the picture, my code is exactly like the example code, but I keep getting an error message, and I can't figure out why.

The only way I could get it to work was copying and pasting the original example and then making some changes. Even after trying to follow directions and making my script look almost exactly like the example (I think mine said "Text to search: " rather than "Text to search through: ") it still didn't work.


#16

why you ended two times?


#17

one is for the .each order
and the other is for the whole order i guess?