i wrote simple block of code expect to- however, @ end of output error message "undefined method `%' nil:nilclass (nomethoderror)". how/why come up, , how can fix it?
def fizzblam range =* (1..100) range.each |i| if range[i] % 7 == 0 && range[i] % 5 == 0 puts "fizzblam" elsif range[i] % 7 == 0 && range[i] % 5 != 0 puts "blam" elsif range[i] % 7 != 0 && range[i] % 5 == 0 puts "fizz" else puts range[i] end end end fizzblam
the elements of range 1 100, indices range 0 99. each method gives elements of array, not indices — use i inside loop body instead of range[i].
you have gotten more reasonable error if indexed array a a.fetch(i) instead of a[i] — former throws indexerror while latter gives nil.
Comments
Post a Comment