1 Replies - 1928 Views - Last Post: 03 March 2008 - 11:43 AM Rate Topic: -----

#1 rubyxpearls  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-February 08

stuck on the last portion of figuring out this ruby code...

Posted 23 February 2008 - 01:26 PM

here's my code so far. I've tried everything to get the last part to work out. Basically, I want my program to print only the numbers up to 3407 where the last digit is equal to the sum of the rest of the digits. so 3407 WOULD print, but not 3406...


100.upto(3407) do |i|
str=i.to_s
arr=str.split("")
print i
print "\t"
sum=0 
sum= arr[arr.length-1]
print sum 
print "\t"
sum2=0
sum3=0
sum2=arr.each{|element|
		sum3+=element.to_i
		}
print sum3
print"\t"
sum4=0
 sum4=((sum3.to_i)-(arr[arr.length-1].to_i))
puts sum4 
puts "\t"




I know that I need to use an if statement, and I've tried " if sum==sum4" but that wasn't working. any suggestions? thanks :D im a beginner so im still taking this all in

Is This A Good Question/Topic? 0
  • +

Replies To: stuck on the last portion of figuring out this ruby code...

#2 AlilSpaced  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 20-December 07

Re: stuck on the last portion of figuring out this ruby code...

Posted 03 March 2008 - 11:43 AM

If your issue is that you are getting an exception that looks something like:

Quote

projectname.rb:20: syntax error, unexpected $end, expecting kEND

Then it may have something to do with the following line of code.
arr=str.split("")

I do not think you can split a string without telling it where it needs to be split in any language. Try fixing that, it might just be the reason your code isn't working.

:pirate: arrrrrgh!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1