JavaScript Program to Condense a Number

You are Here:

Condense a Number

In the following example, we will condense a number (8654) into a single digit (5).

i.e., 8654 = 8 + 6 + 5 + 4
8654 = 23
8654 = 2 + 3
8654 = 5 (single digit)

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <h1>JS Condense a Number</h1> <script> var copyNum, result, remainder; var num = 8654; var userValue = num; while(num > 9) num = myCondense(num); function myCondense(a) { copyNum = a; remainder = 0; result = 0; while(copyNum != 0) { remainder = copyNum % 10; result += remainder; copyNum = Math.floor(copyNum / 10); } return result; } document.write("Single Digit of "+userValue+": "+num); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author