Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Square every digit of a number - JavaScript
We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenated
For example: If the number is −
9119
Then the output should be −
811181
because 9^2 is 81 and 1^2 is 1.
Example
Following is the code −
const num = 9119;
const squared = num => {
const numStr = String(num);
let res = '';
for(let i = 0; i < numStr.length; i++){
const square = Math.pow(+numStr[i], 2);
res += square;
};
return res;
};
console.log(squared(num));
Output
This will produce the following output in console −
811181
Advertisements
