- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Squaring every digit of a number using split() in JavaScript
We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then square every digit of the number, append them and yield the new number.
For example −
If the input number is −
const num = 12349;
Then the output should be −
const output = 1491681;
because '1' + '4' + '9' + '16' + '81' = 1491681
Example
The code for this will be −
const num = 12349; const squareEvery = (num = 1) => { let res = '' const numStr = String(num); const numArr = numStr.split(''); numArr.forEach(digit => { const square = (+digit) * (+digit); res += square; }); return +res; }; console.log(squareEvery(num));
Output
And the output in the console will be −
1491681
- Related Articles
- Square every digit of a number - JavaScript
- Split a URL in JavaScript after every forward slash?
- Greatest digit of a number in JavaScript
- Replace() with Split() in JavaScript to append 0 if number after comma is a single digit
- Explain the diagonal method for squaring a number.
- Digit sum upto a number of digits of a number in JavaScript
- Possible to split a string with separator after every word in JavaScript
- Greater possible digit difference of a number in JavaScript
- Split a range of number to a specific number of intervals JavaScript
- Negative number digit sum in JavaScript
- Finding the largest 5 digit number within the input number using JavaScript
- Number Split into individual digits in JavaScript
- Find the frequency of a digit in a number using C++.
- Is the digit divisible by the previous digit of the number in JavaScript
- Corner digit number difference - JavaScript

Advertisements