本文主要是介绍0029【Edabit ★☆☆☆☆☆】Profitable Gamble,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
0029【Edabit ★☆☆☆☆☆】Profitable Gamble
conditions
math
validation
Instructions
Create a function that takes three arguments
prob
,prize
,pay
and returnstrue
ifprob
*prize
>pay
; otherwise returnfalse
.To illustrate:
profitableGamble(0.2, 50, 9)
… should yield
true
, since the net profit is 1 (0.2 * 50 - 9), and 1 > 0.
Examples
profitableGamble(0.2, 50, 9) // true
profitableGamble(0.9, 1, 2) // false
profitableGamble(0.9, 3, 2) // true
Notes
- A profitable gamble is a game that yields a positive net profit, where net profit is calculated in the following manner:
net_outcome
=probability_of_winning
*prize
-cost_of_playing
.
Solutions
function profitableGamble(prob, prize, pay) {return prob * prize > pay ;
}
TestCases
let Test = (function(){return {assertEquals:function(actual,expected){if(actual !== expected){let errorMsg = `actual is ${actual},${expected} is expected`;throw new Error(errorMsg);}}}
})();Test.assertEquals(profitableGamble(0.2, 50, 9), true)
Test.assertEquals(profitableGamble(0.9, 1, 2), false)
Test.assertEquals(profitableGamble(0.9, 3, 2), true)
Test.assertEquals(profitableGamble(0.33, 10, 3.30), true)
Test.assertEquals(profitableGamble(0, 1000, 0.01), false)
Test.assertEquals(profitableGamble(0.1, 1000, 7), true)
Test.assertEquals(profitableGamble(0, 0, 0), false)
这篇关于0029【Edabit ★☆☆☆☆☆】Profitable Gamble的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!