Repository navigation
#040 Profitable Gamble
Create a function that takes three arguments prob, prize, pay and returns true if prob * prize > pay; otherwise return false..
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/040 Profitable Gamble.js"Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.
This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.
//Create a function that takes three arguments prob, prize, pay and returns true if prob * prize > pay; otherwise return false..
function profitableGamble(prob, prize, pay) {
return prob * prize > pay;
}
import * as Test from '../testHelper.js';
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)Cross-language explanation preserved from the study materials
Reference file: #033 Profitable Gamble.txt. These older notes use a separate numbering scheme and may describe C# code, external diagrams or benchmark timings. They are reference material, not measured JavaScript performance. The current implementation and assertions above are authoritative.
# [Profitable Gamble](https://edabit.com/challenge/ipGXGwbJ3e8aBEu3r)
Published by [supafly](https://edabit.com/user/A9TzwKhGsRfk2D2sk)
conditions math validation
Create a function that takes three arguments `prob`, `prize`, `pay` and returns `true` if `prob * prize > pay`; otherwise return `false`.
To illustrate:
`ProfitableGamble(0.2, 50, 9)`
... should yield `true`, since the net profit is 1 (0.2 * 50 - 9), and 1 > 0.
`ProfitableGamble(0.2, 50, 9) ➞ true`
`ProfitableGamble(0.9, 1, 2) ➞ false`
`ProfitableGamble(0.9, 3, 2) ➞ true`
FPS stands for "frames per second" and it's the number of frames a computer screen shows every second.
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.
### Time Complexity:
The time complexity of `profitableGamble` is _O(1)_. The function performs a fixed number of operations (one multiplication and one comparison) regardless of the input values.
### Space Complexity:
The space complexity is also _O(1)_. The function only requires space for the input parameters (`prob`, `prize`, and `pay`) and the return value (a boolean). No additional memory is allocated that scales with the input size.

//Create a function that takes three arguments prob, prize, pay and returns true if prob * prize > pay; otherwise return false.
using BenchmarkDotNet.Attributes;
using System;
namespace Challenges
{
public class Program33
{
public static bool ProfitableGamble(double prob, int prize, double pay) => prob * prize > pay;
}
public class BenchmarkProgram33
{
[Benchmark]
[Arguments(0.2, 50, 9)]
[Arguments(0.9, 1, 2)]
[Arguments(0.9, 3, 2)]
[Arguments(0.33, 10, 3.30)]
[Arguments(0, 1000, 0.01)]
[Arguments(0.1, 1000, 7)]
[Arguments(0, 0, 0)]
public bool ProfitableGamble(double prob, int prize, double pay) => Program33.ProfitableGamble(prob, prize, pay);
}
}
using System;
using Challenges.Edabit;
using NUnit.Framework;
namespace Tests
{
[TestFixture]
public class Tests33
{
[Test]
[TestCase(0.2, 50, 9, true)]
[TestCase(0.9, 1, 2, false)]
[TestCase(0.9, 3, 2, true)]
[TestCase(0.33, 10, 3.30, true)]
[TestCase(0, 1000, 0.01, false)]
[TestCase(0.1, 1000, 7, true)]
[TestCase(0, 0, 0, false)]
public void ProfitableGamble(double prob, int prize, double pay, bool expectedResult)
{
bool result = Program33.ProfitableGamble(prob, prize, pay);
Assert.That(result, Is.EqualTo(expectedResult));
}
}
}