Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Patrick Shushereba
Patrick Shushereba
10,911 Points

While Loop Sum

I'm trying to write a program to sum numbers, without using an array, and it's not working right. It's only giving me an output of 100.

n = 0
sum = 0
while n <= 99
    n += 1
    sum = n
end

puts sum

Hey! Try it with sum +=....

n = 0
sum = 0
while n <= 99 
  sum += n
  n += 1

end
puts sum

Really close to what you had already! That what you were looking for?

2 Answers

Yw! Would really appreciate it if you'd vote it best answer... thanks! : )

Patrick Shushereba
Patrick Shushereba
10,911 Points

Yes, incrementing sum fixed the problem. I don't know what my problem is. As I'm learning, it seems like the simple things are tripping me up. Thank you.