UnknownDeveloper - 1 year ago 52

Javascript Question

There is a codewars challenge called the Same Birthday Probabilty which challenge is:

Given n number of people in a room, calculate the probability that any

two people in that room have the same birthday (assume 365 days every

year = ignore leap year). Answers should be two decimals unless whole

(0 or 1) eg 0.05

and I did the folloowing for this challenge:

`function calculateProbability(n){`

var result = 0;

for(i=1;i<n;i++){

var total = (365 - i) / 365

result = result * total

}

return Math.round(result)

}

calculateProbability()

With and without the

`Math.round`

✘ Value is not what was expected

and that is the only error.

This is what the test cases are

`Test.expect(calculateProbability(5)==0.03);`

Test.expect(calculateProbability(1000)==1);

Thank you

Answer Source

First of all you should change result to `1`

since multiplying by `0`

always yields zero. The result you are calculating is the complement of the solution, it is the probability that `n`

people have different birthdays. Finally you are not rounding your number to two decimals.

```
function calculateProbability(n){
var result = 1;
for(i=1;i<n;i++){
var total = (365 - i) / 365
result = result * total
}
return Math.round((1-result)*100 )/100
}
```