
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Separating digits of a number in JavaScript
We are required to write a JavaScript program that provides user with a input. When the user inputs some value and press the button, our function should check if the input is a valid number,if it is a valid number, the program should print all digits of the number separately to the screen.
For example − If the input is −
43354
Then the output on the screen should be −
4 3 3 5 4
Let us write the code for this function −
The code for this will be −
HTML
<!DOCTYPE html> <html> <head> <meta charset="utf−8"> <meta name="viewport" content="width=device−width"> <title>Digits</title> </head> <body> <div class="column1"> <div class="input"> <button onclick="perform()"> Enter number </button> </div> <strong><div id="output"> </div></strong> </div> </body> </html>
JS
function perform() { var outputObj = document.getElementById("output"); var a = parseInt(prompt("Please enter a number: ", "")); var digit = ""; outputObj.innerHTML = "" while(a > 0){ let num = a%10 a = Math.floor(a/10) digit += "<p>"+num+"</p>" } outputObj.innerHTML = digit; document.getElementsByTagName("button")[0].setAttribute("disabled","true"); }
And the output on the screen will be −
After clicking OK button,
- Related Articles
- Returning number of digits in factorial of a number in JavaScript
- Recursively adding digits of a number in JavaScript
- Prime digits sum of a number in JavaScript
- Digit sum upto a number of digits of a number in JavaScript
- Finding product of Number digits in JavaScript
- Product sum difference of digits of a number in JavaScript
- Recursive product of all digits of a number - JavaScript
- Convert number to a reversed array of digits in JavaScript
- Destructively Sum all the digits of a number in JavaScript
- Largest product of n contiguous digits of a number in JavaScript
- Number of digits that divide the complete number in JavaScript
- Recursive sum all the digits of a number JavaScript
- Difference between product and sum of digits of a number in JavaScript
- Number Split into individual digits in JavaScript
- Returning number with increasing digits. in JavaScript

Advertisements