<html> <head> <title>Powerball Ticket Simulator</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css"> <link rel="stylesheet" href="style.css" type="text/css"> <script src="angular.min.js"></script> <script src="powerball.js"></script> </head> <body ng-app="app"> <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">Powerball Simulator</a> </div> </div> </nav> <div id="main" class="container"> <div class="row"> <div class="col-md-6" ng-controller="TicketController"> <h4 class="page-header">Select your numbers</h4> <form name="BuyTicketForm"> <a href ng-click="Tickets.add(BuyTicketCount)" class="btn btn-primary" ng-disabled="BuyTicketForm.BuyTicketCount.$error.pattern"> Buy additional tickets: </a> <input name="BuyTicketCount" class="buy-ticket-count" maxlength="5" ng-model="BuyTicketCount" ng-change="LimitBuyTicketCount()" ng-pattern="/^\d+$/"> </form> <table class="table table-condensed my-numbers"> <tbody> <tr ng-repeat="(key, Ticket) in Tickets.list" ng-if="Tickets.list.length < 50"> <th>{{key + 1}}</th> <td><input type="text" ng-model="Ticket.one"></td> <td><input type="text" ng-model="Ticket.two"></td> <td><input type="text" ng-model="Ticket.three"></td> <td><input type="text" ng-model="Ticket.four"></td> <td><input type="text" ng-model="Ticket.five"></td> <td class="warning"><input type="text" ng-model="Ticket.powerball"></td> <td> <button ng-click="Tickets.delete(key)" class="btn btn-danger btn-sm">x</button> </td> </tr> </tbody> <tfoot> <tr> <td colspan="8" class="info"> <strong>Cost per draw:</strong> {{Tickets.costPerDraw | currency}} </td> </tr> </tfoot> </table> </div> <div class="col-md-6" ng-controller="LotteryController"> <h4 class="page-header">Lottery Results</h4> <div class="btn-group"> <button ng-click="Lottery.Draw()" class="btn btn-default">Draw</button> <button ng-click="Lottery.Start()" class="btn btn-default">Start (Automatic)</button> <button ng-click="Lottery.Stop()" class="btn btn-default">Stop</button> </div> <h4 class="page-header">Total Winnings</h4> <table class="table table-condensed historic-winning-numbers"> <thead> <tr> <th>Numbers Matched</th> <th>Total Wins</th> <th>Total Payout</th> </tr> </thead> <tbody> <tr> <th>5 Numbers + PowerBall</th> <td>{{Lottery.Wins['5+1']}}</td> <td>{{Lottery.Wins['5+1'] * Lottery.PrizeAmounts['5+1'] | currency}}</td> </tr> <tr> <th>5 Numbers</th> <td>{{Lottery.Wins['5+0']}}</td> <td>{{Lottery.Wins['5+0'] * Lottery.PrizeAmounts['5+0'] | currency}}</td> </tr> <tr> <th>4 Numbers + PowerBall</th> <td>{{Lottery.Wins['4+1']}}</td> <td>{{Lottery.Wins['4+1'] * Lottery.PrizeAmounts['4+1'] | currency}}</td> </tr> <tr> <th>4 Numbers</th> <td>{{Lottery.Wins['4+0']}}</td> <td>{{Lottery.Wins['4+0'] * Lottery.PrizeAmounts['4+0'] | currency}}</td> </tr> <tr> <th>3 Numbers + PowerBall</th> <td>{{Lottery.Wins['3+1']}}</td> <td>{{Lottery.Wins['3+1'] * Lottery.PrizeAmounts['3+1'] | currency}}</td> </tr> <tr> <th>3 Numbers</th> <td>{{Lottery.Wins['3+0']}}</td> <td>{{Lottery.Wins['3+0'] * Lottery.PrizeAmounts['3+0'] | currency}}</td> </tr> <tr> <th>2 Numbers + PowerBall</th> <td>{{Lottery.Wins['2+1']}}</td> <td>{{Lottery.Wins['2+1'] * Lottery.PrizeAmounts['2+1'] | currency}}</td> </tr> <tr> <th>1 Numbers + PowerBall</th> <td>{{Lottery.Wins['1+1']}}</td> <td>{{Lottery.Wins['1+1'] * Lottery.PrizeAmounts['1+1'] | currency}}</td> </tr> <tr> <th>0 Numbers + PowerBall</th> <td>{{Lottery.Wins['0+1']}}</td> <td>{{Lottery.Wins['0+1'] * Lottery.PrizeAmounts['0+1'] | currency}}</td> </tr> </tbody> <tfoot> <tr class="info"> <th>Total Winnings:</th> <td>{{Lottery.TotalWins}}</td> <td>{{Lottery.TotalWinningAmount | currency}}</td> </tr> <tr class="active"> <th>Total Purchased:</th> <td colspan="2">{{Lottery.TotalPurchaseAmount | currency}}</td> </tr> </tfoot> </table> </div> </div> </div> </body> </html>

/* Styles go here */ .my-numbers th { width: 25px; vertical-align: middle; } .my-numbers td { width: 50px; vertical-align: middle; } .my-numbers input { width: 40px; } .buy-ticket-count { width: 60px; }

'use strict'; (function () { var COST_PER_PLAY = 2; var PLAYS_PER_YEAR = 52 * 2; var PICK_5_OF = 69; var PICK_1_OF = 26; var PRIZE_AMOUNTS = { '0+1': 4, '1+1': 4, '2+1': 7, '3+0': 7, '3+1': 100, '4+0': 100, '4+1': 50000, '5+0': 1000000, '5+1': 800000000 }; var app = angular.module('app', []); app.controller('LotteryController', function ($scope, LotteryService) { $scope.Lottery = LotteryService; }); app.service('LotteryService', function ($interval, RandomNumberService, TicketCollection) { return new LotteryFactory; //////////// function LotteryFactory () { var StartDrawInterval; var self = this; self.PrizeAmounts = PRIZE_AMOUNTS; self.PastWinningNumbers = []; self.TotalPurchaseAmount = 0; self.TotalWins = 0; self.Wins = { '5+1': 0, '5+0': 0, '4+1': 0, '4+0': 0, '3+1': 0, '3+0': 0, '2+1': 0, '2+0': 0, '1+1': 0, '0+1': 0 }; self.TotalWinningAmount = 0; self.Draw = Draw; self.Start = Start; self.Stop = Stop; //////////// function Draw () { var WinningNumbers = { Pick5: RandomNumberService('PICK_5'), PowerBall: RandomNumberService('PICK_1')[0] }; self.PastWinningNumbers.push(WinningNumbers); self.TotalPurchaseAmount += TicketCollection.costPerDraw; FindWinningTickets(WinningNumbers); } function Start () { var IsFewTickets = TicketCollection.count <= 100; StartDrawInterval = $interval(IsFewTickets ? MultipleDraw : Draw, 1); function MultipleDraw () { for (var n = 0; n < 100; n += 1) Draw(); } } function Stop () { $interval.cancel(StartDrawInterval); } function FindWinningTickets (WinningNumbers) { var Tickets = TicketCollection.list; for (var i = 0; i < Tickets.length; i += 1) { var Ticket = Tickets[i]; var PickFiveMatches = HowManyPickFiveMatches(Ticket); var PowerBallMatches = HowManyPowerBallMatches(Ticket); var TicketResults = PickFiveMatches + '+' + PowerBallMatches; if (self.PrizeAmounts[TicketResults]) { self.Wins[TicketResults]++; self.TotalWinningAmount += self.PrizeAmounts[TicketResults]; } } //////////// function HowManyPickFiveMatches (Ticket) { var matches = 0; for (var prop in Ticket) { if (prop === 'powerball') continue; var SelectedNumber = Ticket[prop]; var IsWinningNumber = WinningNumbers.Pick5.indexOf(SelectedNumber) !== -1; if (IsWinningNumber) matches++; } return matches; } function HowManyPowerBallMatches (Ticket) { return Ticket.powerball === WinningNumbers.PowerBall ? 1 : 0; } } } }); app.controller('TicketController', function ($scope, TicketCollection) { $scope.BuyTicketCount = 1; $scope.LimitBuyTicketCount = LimitBuyTicketCount; $scope.Tickets = TicketCollection; $scope.Tickets.add($scope.BuyTicketCount); $scope.CostPerPlay = parseFloat(COST_PER_PLAY); //////////// function LimitBuyTicketCount () { var BuyTicketCount = parseInt($scope.BuyTicketCount); if (BuyTicketCount > 25000) $scope.BuyTicketCount = 25000; } }); app.service('TicketCollection', function (RandomNumberService) { return new TicketCollectionFactory; //////////// function TicketCollectionFactory () { var self = this; self.count = 0; self.costPerDraw = 0; self.list = []; self.add = Create; self.delete = Delete; //////////// function Create (BuyTickets) { for (var n = 0; n < BuyTickets; n += 1) { self.list.push(new Ticket); } UpdateValues(); } function Delete (index) { self.list.splice(index, 1); UpdateValues(); } function UpdateValues () { self.count = self.list.length; self.costPerDraw = self.count * parseFloat(COST_PER_PLAY); } } function Ticket () { var pickFiveNumbers = RandomNumberService('PICK_5'); var pickOneNumbers = RandomNumberService('PICK_1'); this.one = pickFiveNumbers[0]; this.two = pickFiveNumbers[1]; this.three = pickFiveNumbers[2]; this.four = pickFiveNumbers[3]; this.five = pickFiveNumbers[4]; this.powerball = pickOneNumbers[0]; } }); app.service('RandomNumberService', function () { return randomNumberGenerator; //////////// function makePickList (listType) { switch (listType) { case 'PICK_5': return createArrayFromOneToMaximum(PICK_5_OF); case 'PICK_1': return createArrayFromOneToMaximum(PICK_1_OF); } //////////// function createArrayFromOneToMaximum (maximum) { var list = []; for (var n = 1; n <= maximum; n += 1) list.push(n); return list; } } function randomNumberGenerator (listType) { var pickNumberOptions = makePickList(listType); var selectedNumbers = []; while (selectedNumbers.length < 5) { var listIndex = Math.floor(Math.random() * pickNumberOptions.length); var selectedNumber = pickNumberOptions[listIndex]; selectedNumbers.push(selectedNumber); pickNumberOptions.splice(listIndex, 1); } return selectedNumbers; } }); })();