Given a number x, find sum of digits in that number.

Examples:

1. Input n = 8

Output – Sum of digits in number is 8

2. Input n = 1234

Output – Sum of digits in number is 10

3. Input n = – 8

Output – Sum of digits in number is -8

4. Input n = -1234

Output – Sum of digits in number is 8

Step 1: we have to create a demo.html

Step 2: we write the function in javascript to sum of all digits in a number. With 2 ways to declare the function.

The first one: using **var num = parseInt(document.getElementById(“demo”).value);** when everything is done, we got the sum. We will use **document.getElementById(“result”).innerHTML = sum** to show on web browser.

The second one: using **var textType = Node.textContent ? ‘textContent’ : ‘innerText’;** Is is the textContent or innerText? You also do something like the first one, but when you get the value for <p> tag. Just using like this **result1[textType] = sum;**

Let’s do it now

###

[Javascript] How to sum of all its digits for a number

Top books