-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsquare-every-digit.js
37 lines (30 loc) · 1010 Bytes
/
square-every-digit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function squareDigits(num){
// a place to store the squared numbers
let squaredNumbers = '';
// iterate over each digit in the number
while(num > 0) {
let digit = num % 10;
// square the digit
let squaredDigit = Math.pow(digit, 2);
// prepend to where we are storing the squared numbers
squaredNumbers = squaredDigit + squaredNumbers;
num = Math.floor(num / 10);
}
// return the squared numbers as a number
return Number(squaredNumbers);
}
function squareDigits(num){
return +num.toString().split('').map(digit => Math.pow(digit, 2)).join('');
}
function squareDigits(num) {
return +num.toString().split('').reduce((squaredNumbers, digit) => {
return squaredNumbers + Math.pow(digit, 2);
}, '');
}
function squareDigits(num) {
return +Array.prototype.reduce.call(num.toString(), (squaredNumbers, digit) => {
return squaredNumbers + Math.pow(digit, 2);
}, '');
}
console.log(squareDigits(9119), 811181)
console.log("WHY IS THIS NOT WORKING??")