-
Notifications
You must be signed in to change notification settings - Fork 0
/
Square Every Digit.js
66 lines (49 loc) · 1.4 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
In this exercise you are asked to square every digit of a number.
For example, if we run 9119 through the function, 811181 will come out, because 92 is 81 and 12 is 1.
Note: The function accepts an integer and returns an integer
https://www.codewars.com/kata/square-every-digit/javascript
*/
// My solution
function squareDigits(num) {
// 1. Convert number to string
const string = num.toString();
// 2. Convert string into array of individual characters
const arrayOfDigits = [...string];
let computedArray = [];
// 3. Push each computed/squared value into a new array
arrayOfDigits.forEach(number => {
computedArray.push(Math.pow(Number(number), 2));
});
let combinedArray = "";
// 4. Combine each charter in the computedArray into a single string value
computedArray.forEach(char => {
return (combinedArray = combinedArray + char);
});
// 5. Convert the string value to a number and return it
const finalNumber = Number(combinedArray);
return finalNumber;
}
// OTHER CLEVER SOLUTIONS AND BEST PRACTICES:
// 1
function squareDigits(num) {
return Number(
("" + num)
.split("")
.map(function(val) {
return val * val;
})
.join("")
);
}
// 2
function squareDigits(num) {
var array = num
.toString()
.split("")
.map(function(int) {
var i = parseInt(int);
return i * i;
});
return parseInt(array.join(""));
}