# A JavaScript Betting Calculator

A JavaScript Betting Calculator

I am far from a big gambler, and am well aware of how harmful a habit gambling can be. Nevertheless last year I accepted an invitation to join a small betting syndicate by some close friends. Basically we each contribute £5 each week, and take it in turns to gamble the week’s pot. It is good fun and most importantly it helps us all to stay in touch. Since joining I have naturally taken more of an interest in how betting works, in particular accumulators and the various combinations of accumulators that can be placed. It became clear that for these bets it is far from straightforward to calculate potential returns. I thought it would be a fun exercise to write up a bet calculator function in JavaScript. Not only is this an area which I have taken something of an interest in, but it is actually a quite interesting mathematical puzzle.

Warning – whilst writing up this post I realised that much of the code would be completely meaningless to anyone who didn’t understand the basics of betting in the UK (fractional odds, accumulators and full cover bets). Therefore before we start talking about code, I will provide a brief (at least as brief as I could manage) overview of these concepts. As a consequence this has turned out to be an unusually long post, so apologies in advance. An alternative overview of betting odds can be found here .

### A Quick Guide to Betting in the UK

In the UK, betting odds are fractional. So for an event which has a 25% chance of occurring, this would be expressed as a price of 3/1. For each pound you place on a 3/1 bet, you will receive your pound stake back plus an additional 3 pounds should your bet win. For example, if you place a £5 stake on a 3/1 bet and it wins, you will receive a total of

£20. This represents a 25% chance, as you will break even in the long term if your bet wins 25% of the time. A bet on a single event like this is known as a single .

It follows that the calculation of winnings can be expressed by the following formula:

r = s + (s*n/d)

where r = potential returns, s = stake, n = numerator part of odds, d = denominator part of odds.

So using this in the example above:

r = 5 + (5*3/1) = 20

Things become more complex with accumulator bets. With an accumulator bet you are betting on more than one event – your bet only wins if all your selections win. The simplest accumulator bet is a double. where you bet on 2 events occurring. An accumulator consisting of 3 selections is a treble . 4 selections is a fourfold, 5 is a fivefold and so on.

The easiest way to calculate the potential returns of an accumulator bet is to first convert the fractional odds for each selection into decimal odds. The overall odds of the accumulator winning is then the product of all the decimal odds in your accumulator. We saw above that a 3/1 price represents a 25% chance of winning, so its decimal odds are 0.25. It follows that fractional odds can be converted to decimal odds with the formula:

dec = (n + d)/d

where dec = decimal odds, n = numerator, d = denominator

For example, let’s say you have placed a 4-fold bet, consisting of four selections which are priced 5/2, 11/4, 8/1 and 1/2, and a stake of 4 pounds. The decimal odds of each bet are 3.5, 3.75, 9 and 1.5 respectively. Multiplying these together we have a total accumulator decimal price of 117.1875. Multiply this by our stake amount and we have potential winnings of 708.75.

Here are some more examples:

Source: www.codeproject.com

Category: Forex