-
Notifications
You must be signed in to change notification settings - Fork 1
/
DEV#173.js
61 lines (55 loc) · 1.36 KB
/
DEV#173.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
// https://dev.to/thepracticaldev/daily-challenge-173-pandemia-5ae7
function doomPercentage (s) {
let infected = 0
let total = 0
if (!s.includes('1') || !s.includes('0')) {
return 0
}
s.split('X').forEach(continent => {
if (continent.includes('1')) {
continent = continent.replace(/0/gi, '1')
infected += continent.length
}
total += continent.length
})
return 100 * infected / total
};
// Tests
const cases = [
{
label: '01000000X000X011X0X',
input: doomPercentage('01000000X000X011X0X'),
shouldBe: 73.33333333333333
},
{
label: '01X000X010X011XX',
input: doomPercentage('01X000X010X011XX'),
shouldBe: 72.72727272727273
},
{
label: 'XXXXX',
input: doomPercentage('XXXXX'),
shouldBe: 0
},
{
label: '00000000X00X0000',
input: doomPercentage('00000000X00X0000'),
shouldBe: 0
},
{
label: '0000000010',
input: doomPercentage('0000000010'),
shouldBe: 100
},
{
label: '000001XXXX0010X1X00010',
input: doomPercentage('000001XXXX0010X1X00010'),
shouldBe: 100
},
{
label: 'X00X000000X10X0100',
input: doomPercentage('X00X000000X10X0100'),
shouldBe: 42.857142857142854
}
]
cases.map(test => console.log((test.input === test.shouldBe ? '✅' : '❗') + ' ' + test.label + ' => ' + 'Should be: ' + test.shouldBe + ' received: ' + test.input))