With independent layers, the chance an attack gets through is the product of the chances it passes each one.
What to build
Read how much each layer stops, and print how much still gets through after each layer.
Requirements
- Each line is one layer's stop rate, a whole percentage from 0 to 100.
- After each layer, print
after layer N: P%, where P is the share still getting through, with exactly 2 decimal places. - At the end, print
about X in 10000, with X the expected number of attacks out of 10000 that get through, rounded with Math.round.
Input
One layer per line.
Output
One line per layer, then the summary line.
Constraints
1 to 6 layers.
Example
Input:
90
60
99
Output:
after layer 1: 10.00%
after layer 2: 4.00%
after layer 3: 0.04%
about 4 in 10000
Why it works
Passing every layer means passing the first and the second and so on, so for independent layers the chances multiply; each layer keeps (100 − stop)% of what reaches it.
Examples
Example 1 · Input
90 60 99
Expected output
after layer 1: 10.00% after layer 2: 4.00% after layer 3: 0.04% about 4 in 10000
Example 2 · Input
50 50
Expected output
after layer 1: 50.00% after layer 2: 25.00% about 2500 in 10000
Hints
Hint 1
Start with through = 1 and multiply by (100 − stop) / 100 for each layer.
Hint 2
Multiply by 100 before toFixed(2) to print a percentage.
Solution
Show one correct solution (try it yourself first)
let through = 1;
let n = 0;
let line = prompt();
while (line !== null) {
n++;
through *= (100 - Number(line)) / 100;
console.log(`after layer ${n}: ${(through * 100).toFixed(2)}%`);
line = prompt();
}
console.log(`about ${Math.round(through * 10000)} in 10000`);
▶ Open this solution in the compiler, then press Visualize to watch it run step by step