Task of Challenge #17/54

Calculate the sum of digits of a positive integer number.

The integer 1235321

Solution

Explanation

The solution is pretty straightforward. You need to separate the digits to loop over them and add them to a sum.

You can do it by a conversion to a string because this is nothing more than a char array under the hood.

You can take advantage of this knowledge by iterating over the array with a for-of-loop. While looping you add up the sum and finally returning the sum to the caller.

This is the printout after the sum was created.

Pro-Tip: If you want to write better code, try to accomplish this task with the reduce() and map() functions from the Array.prototype.

Follow me on Instagram and don’t miss the latest Challenge!

https://www.instagram.com/arnold.code/

Become a bullet proof developer and seek my coding adventures on Udemy

https://www.udemy.com/user/arnold-abraham-3/

Don’t Miss My Most Read Article with over 8000 Views

One Reason Not to Migrate From JavaScript to TypeScript

Typescript saves a lot of development time, but is it worth an entire migrating process?

Get Your JavaScript Coding To A Very Professional Level with this article

Upgrade Your Legacy Coding Style With These 6 Modern Ways of Coding

And How These 3 Famous TV Shows Make You Write Excellent JavaScript Code