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
• Paul Clapham
• Liutauras Vilda
• Knute Snortum
• Bear Bibeault
Sheriffs:
• Devaka Cooray
• Jeanne Boyarsky
• Junilu Lacar
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Carey Brown
• salvin francis
Bartenders:
• Tim Holloway
• Piet Souris
• Frits Walraven

# Finding sum of digits of a number until sum becomes single digit

Ranch Hand
Posts: 202
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: 67365
173
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: 202

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

Bear Bibeault
Marshal
Posts: 67365
173
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: 202

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: 67365
173

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: 202

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: 25198
64
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: 12797
51
• 1
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