ruby - program using while with array values -


i trying write small program goes through array's values outputting each individual value. when reaches 15 stops , outputs "too big".

why logic below wrong, makes sense me..

x = [10,11,12,13,14,15,16,17,18]  def counting     x[y]     while y < 15         puts y     else         puts "too big"   end  puts counting 

i'm learning sorry if simple solution.

if want 1 liner:

x.each {|y| y < 15 ? puts(y) : puts("too big")  ||  break  } 

if insist using while, can done following:

i = 0 while   x[i] < 15 ?  puts(x[i]) :  puts("too big")  ||  break   i+=1 end 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -