0
Why does this only output the x value??
x = gets for y in (1..12) z = x * y puts z end this is a code which i created for multiplication table. but this only outputs the input x. help me to make this work
3 Respostas
+ 1
x = gets
x = x.to_i
for y in (1..12)
z = x * y
puts "#{x} * #{y} = #{z}"
end
you need to convert x to number because the interpreter thinks it's a string so "5"' * 1 = "5"
and "5" * 2 = "55" (each digit printed on a new line)
+ 1
no problem, its called string interpolation, what it does is it says get the variable between these curly braces and put its value here in the string, eg
message = "hello"
puts "#{message} world" # hello world
message = "good afternoon"
puts "#{message} world" # good afternoon world
alternatively you can just concatenate them like this: message + " world" but when you introduce more variables it gets more complicated to do so which is why people like to use interpolation to make things easier
0
y are those in between curly brakets? i am just new to coding. so please :) ur helps are valuable