While loop does only 1 loop


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-mzrZ6/0/6?curriculum_id=5059f8619189a5000201fbcb


While loop does only 1 loop and does not repeat the block of codes under it. Can someone please explain why this is happening?


puts "Enter your text:"
text = gets.chomp
text.downcase!
puts "How many words do you have to hide?"
num = gets.to_i
red_text = ""

while num > 0
    puts "Word to be kept secret:"
    redact = gets.chomp!
    redact.downcase!
    words = text.split (" ")

    words.each do |word|
        if word != redact
            red_text.concat(word + " ")
        else 
           red_text.concat(" REDACTED ")
        end

        num -= 1
    end
   
    puts red_text
end


#2

Hint: You should subtract 1 from num inside the while loop, not the .each block. Do you know why? :wink:


#3

thanks for that, yeah makes sense