-
Notifications
You must be signed in to change notification settings - Fork 0
/
7kyu-balanced-number.js
82 lines (55 loc) · 2.73 KB
/
7kyu-balanced-number.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Definition
// Balanced number is the number that * The sum of all digits to the left of the middle digit(s) and the sum of all digits to the right of the middle digit(s) are equal*.
// Task
// Given a number, Find if it is Balanced or not .
// Warm-up (Highly recommended)
// Playing With Numbers Series
// Notes
// If the number has an odd number of digits then there is only one middle digit, e.g. 92645 has middle digit 6; otherwise, there are two middle digits , e.g. 1301 has middle digits 3 and 0
// The middle digit(s) should not be considered when determining whether a number is balanced or not, e.g 413023 is a balanced number because the left sum and right sum are both 5.
// Number passed is always Positive .
// Return the result as String
// Input >> Output Examples
// 1- balancedNum (7) ==> return "Balanced" .
// Explanation:
// Since , The sum of all digits to the left of the middle digit (0)
// and the sum of all digits to the right of the middle digit (0) are equal , then It's Balanced
// 2- balancedNum (295591) ==> return "Not Balanced" .
// Explanation:
// Since , The sum of all digits to the left of the middle digits (11)
// and the sum of all digits to the right of the middle digits (10) are equal , then It's Not Balanced
// Note : The middle digit(s) are 55 .
// 3- balancedNum (959) ==> return "Balanced" .
// Explanation:
// Since , The sum of all digits to the left of the middle digits (9)
// and the sum of all digits to the right of the middle digits (9) are equal , then It's Balanced
// Note : The middle digit is 5 .
// 4- balancedNum (27102983) ==> return "Not Balanced" .
// Explanation:
// Since , The sum of all digits to the left of the middle digits (10)
// and the sum of all digits to the right of the middle digits (20) are equal , then It's Not Balanced
// Note : The middle digit(s) are 02 .
// Playing with Numbers Series
// Playing With Lists/Arrays Series
// For More Enjoyable Katas
// ALL translations are welcomed
// Enjoy Learning !!
// Zizou
function balancedNum(number) {
const stringArray = String(number).split('');
const numberArray = stringArray.map(value => Number(value));
let leftSum = 0;
let rightSum = 0;
if (numberArray.length === 1 || numberArray.length === 2) {
return 'Balanced';
}
// Math.ceil is for the case when the number is not even
for (let i = 0; i < Math.ceil(numberArray.length / 2 - 1); i += 1) {
leftSum += numberArray[i];
rightSum += numberArray[numberArray.length - (i + 1)];
}
if (leftSum === rightSum) {
return 'Balanced';
}
return 'Not Balanced';
}