-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSevenSegmentDisplay.js
More file actions
161 lines (153 loc) · 4.6 KB
/
Copy pathSevenSegmentDisplay.js
File metadata and controls
161 lines (153 loc) · 4.6 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Description:
*
* A Seven Segment Display is an electronic display device, used
* to display decimal or hexadecimal numerals. It involves
* seven led segments that are lit in specific combinations
* to represent a specific numeral. An example of
* each combination is shown below:
*
* seven segment display
*
* For this Kata, you must accept an integer
* in the range 0 - 999999 and print it as
* a string (in decimal format), with each digit being
* represented as its own seven segment
* display (6x seven segment displays in total). Each of
* the individual led segments per display should be
* represented by three hashes ###. Vertical bars | (ASCII 124)
* represent the edges of each display, with a
* single whitespace on either side between the edge and the
* area of the led segments. An example of the
* expected output is shown below:
*
* segment_display(123456) =
* | ### | ### | | ### | ### |
* | # | # | # | # # | # | # |
* | # | # | # | # # | # | # |
* | # | # | # | # # | # | # |
* | | ### | ### | ### | ### | ### |
* | # | # | # | # | # | # # |
* | # | # | # | # | # | # # |
* | # | # | # | # | # | # # |
* | | ### | ### | | ### | ### |
*
*
* For clarity, the entire set of required combinations is provided below:
*
* | ### | | ### | ### | | ### | ### | ### | ### | ### |
* | # # | # | # | # | # # | # | # | # | # # | # # |
* | # # | # | # | # | # # | # | # | # | # # | # # |
* | # # | # | # | # | # # | # | # | # | # # | # # |
* | | | ### | ### | ### | ### | ### | | ### | ### |
* | # # | # | # | # | # | # | # # | # | # # | # |
* | # # | # | # | # | # | # | # # | # | # # | # |
* | # # | # | # | # | # | # | # # | # | # # | # |
* | ### | | ### | ### | | ### | ### | | ### | ### |
*
*
* If the number is smaller than 6 digits, the result
* should be justified to the right, with the
* unused segments being blank (as they are not turned on).
* Refer to the sample test for an example.
*
*
* Note: There should not be any trailing whitespace for any line.
*
* Please rate and enjoy!
*
* Kata URL: https://www.codewars.com/kata/5d7091aa7bf8d0001200c133
*
* @param {*} num
* @returns
*/
function segmentDisplay(num) {
num = String(num).split("");
let result = [];
let finalResult = [];
//row 0
num.forEach((n, i) => {
if (n === "1" || n === "4") {
result.push(" ");
} else {
result.push(" ### ");
}
if (i === 5) {
finalResult.push("|");
finalResult.push(result.join("|"));
finalResult.push("|\n");
result = [];
}
});
//row 2-4
let counter = 0;
while (counter < 3) {
num.forEach((n, i) => {
if (n === "1" || n === "2" || n === "3" || n === "7") {
result.push(" # ");
} else if (n === "5" || n === "6") {
result.push(" # ");
} else {
result.push(" # # ");
}
if (i === 5) {
finalResult.push("|");
finalResult.push(result.join("|"));
finalResult.push("|\n");
result = [];
}
});
counter++;
}
//row 5
num.forEach((n, i) => {
if (n === "0" || n === "1" || n === "7") {
result.push(" ");
} else {
result.push(" ### ");
}
if (i === 5) {
finalResult.push("|");
finalResult.push(result.join("|"));
finalResult.push("|\n");
result = [];
}
});
//row 6-8
let counter1 = 0;
while (counter1 < 3) {
num.forEach((n, i) => {
if (n === "0" || n === "6") {
result.push(" # # ");
} else if (n === "2") {
result.push(" # ");
} else {
result.push(" # ");
}
if (i === 5) {
finalResult.push("|");
finalResult.push(result.join("|"));
finalResult.push("|\n");
result = [];
}
});
counter1++;
}
//row 9
num.forEach((n, i) => {
if (n === "1" || n === "4" || n === "7") {
result.push(" ");
} else {
result.push(" ### ");
}
if (i === 5) {
finalResult.push("|");
finalResult.push(result.join("|"));
finalResult.push("|\n");
result = [];
}
});
let x = finalResult.join("");
console.log(x);
return x;
}