Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Finding sum of digits of a number until sum becomes single digit  RSS feed

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to solve the above mentioned problem Like if given number is 123, number of times required to convert it into a single digit number is 1 (1+2+3=6).



can you tell where i am going wrong ?
 
Marshal
Posts: 67243
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a return statement, but no function signature. Please repost the code such that it shows the complete function.
 
priyanshi bhardwaj
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote: Please repost the code such that it shows the complete function.



 
Bear Bibeault
Marshal
Posts: 67243
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My advice would be to step away from the code for a minute and write, in English, the list of steps to take to accomplish this. Then, and only then, write code to implement it.
 
priyanshi bhardwaj
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:My advice would be to step away from the code for a minute and write, in English, the list of steps to take to accomplish this. Then, and only then, write code to implement it.


I am facing problem in how to  calculate the no of times.Like sum can be calculated but how to calculate no of times the number will get replaced.I have referred geeksforgeeks for this problem GEEKSFORGEEKS I am not getting how to implement that explanation in javascript
 
Bear Bibeault
Marshal
Posts: 67243
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

priyanshi bhardwaj wrote: I am not getting how to implement that explanation in javascript


Do you understand the explanation? Are the steps to achieve the algorithm clear?

Again, write out, in your own words, how to solve the problem. Until you have that, any attempt to write code to implement an algorithm that is not understood will be futile.
 
priyanshi bhardwaj
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

priyanshi bhardwaj wrote: I am not getting how to implement that explanation in javascript


Do you understand the explanation? Are the steps to achieve the algorithm clear?

Again, write out, in your own words, how to solve the problem. Until you have that, any attempt to write code to implement an algorithm that is not understood will be futile.


I have tried this
 
Marshal
Posts: 24198
54
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't much look like "your own words". That looks like some kind of programming.

But let's review it. Start by lines 2, 3, and 4. What is the explanation in words of those three lines?
 
lowercase baba
Posts: 12726
51
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are codinig, you should spend 90% of your time THINKING, and only 10% TYPING.  Never write a single line of code until you have a good idea in your head how you are going to solve the problem.

I generally start by writing out the steps I take in English, which is my native language.  Then I refine them, breaking them down into smaller and smaller pieces, making my description more and more detailed.  for this problem, it'd be something like this:

1)  get a number
2)  if the number is not a single digit
3)  add up all the digits in the number to get a new number
4)  return to step 2
5)  if it is a single digit, we're done.

Ok, so i look at that.  line 1 looks like it should be a method all to itself.  Based on the code sample you provided, i'd assume that's already done.
Now, lined 2-4 look like they should be a loop of some kind. Line 3 all by itself looks like it should be a method.

now I can see writing a method that has a loop to see if I have a single digit, and return that if so.  If not, I need to call another method that sums the digits.

So now I can break line 3 into individual steps.  It's a method all to itself, so it doesn't care about how many times I've tried, how long the result is, or anything. All it has to do is sum the digits in whatever number it is given.  Now I can code the method with the loop, or code this method.  Whichever way I go, I can write some temporary code as a harness to test the piece I'm working on.  Once I'm sure that piece works, I can then use it as I build the other pieces.

The idea is to separate out responsibility of tasks, so each method does one simple thing, does it well,  and can be easily understood.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!