Background
I need a web app that I can enter the last date that a Bingo Jackpot was last won. The jackpot of 500 euro is won if someone fills a bingo panel in fewer than 50 calls in the first bingo game. If the jackpot isn’t won 50 euro is added to the jackpot and one call is removed from the number of calls required. I’d like to make an app that can be accessed on a phone. you enter the date that the jackpot was last won and it lists out the jackpot and number of calls each week from then to the day it is run.
Description
The calculateJackpot()
function calculates and displays the jackpot amount and number of calls for a certain number of weeks since the last jackpot win. Here’s a breakdown of how it works:
1. Get the Last Won Date:
const lastWonDate = new Date(document.getElementById("lastWonDate").value);
- This line gets the date entered by the user in an input field with the ID "lastWonDate" and converts it into a
Date
object.
- This line gets the date entered by the user in an input field with the ID "lastWonDate" and converts it into a
2. Calculate Weeks Since Last Win:
const today = new Date();
- Gets today’s date.
const weeksSinceWin = Math.ceil((today - lastWonDate) / (7 * 24 * 60 * 60 * 1000));
- Calculates the difference in milliseconds between today and the last won date.
- Divides the difference by the number of milliseconds in a week to get the number of weeks.
Math.ceil()
rounds the result up to the nearest whole number to get the number of whole weeks since the last win.
3. Initialize Variables:
let jackpot = 500;
- Sets the initial jackpot amount to €500.
let calls = 50;
- Sets the initial number of calls to 50.
let resultsHTML = "";
- Creates an empty string to store the HTML output.
4. Loop Through Weeks:
for (let i = 1; i <= weeksSinceWin; i++) { ... }
- This loop iterates through each week since the last win, starting from week 1.
5. Calculate Date for Each Week:
const currentDate = new Date(lastWonDate);
- Creates a new
Date
object for the current week, starting with the last won date.
- Creates a new
currentDate.setDate(lastWonDate.getDate() + (i * 7));
- Adds
i * 7
days to the last won date to get the date of the current week.
- Adds
6. Format the Date:
const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
- Defines options for formatting the date in European format (dd/mm/yyyy).
const formattedDate = currentDate.toLocaleDateString('en-GB', options);
- Formats the
currentDate
using the specified options.
- Formats the
7. Build the Output HTML:
resultsHTML += Week ${i} (${formattedDate}): Jackpot - €${jackpot}, Calls - ${calls}<br>
;`- Appends a line of HTML to
resultsHTML
with the week number, formatted date, current jackpot amount, and number of calls.
- Appends a line of HTML to
8. Update Jackpot and Calls:
jackpot += 50;
- Increases the jackpot by €50 for each week.
calls -= 1;
- Decreases the number of calls by 1 for each week.
9. Display the Results:
document.getElementById("results").innerHTML = resultsHTML;
- Sets the
innerHTML
of an HTML element with the ID "results" to display the generatedresultsHTML
.
- Sets the
In summary, this function takes a date input, calculates how many weeks have passed since that date, and then generates a report showing how the jackpot and number of calls would change over those weeks.
Code
function calculateJackpot() {
const lastWonDate = new Date(document.getElementById("lastWonDate").value);
const today = new Date();
const weeksSinceWin = Math.ceil((today - lastWonDate) / (7 * 24 * 60 * 60 * 1000));
let jackpot = 500;
let calls = 50;
let resultsHTML = "";
for (let i = 1; i <= weeksSinceWin; i++) {
// Calculate the date for the current week
const currentDate = new Date(lastWonDate);
currentDate.setDate(lastWonDate.getDate() + (i * 7)); // Add i weeks to the last won date
// Format the date (e.g., "dd/mm/yyyy") - adjust to your preferred format
const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
const formattedDate = currentDate.toLocaleDateString('en-GB', options); // Using European date format
resultsHTML += `Week ${i} (${formattedDate}): Jackpot - €${jackpot}, Calls - ${calls}<br>`;
jackpot += 50;
calls -= 1;
}
document.getElementById("results").innerHTML = resultsHTML;
}
HTML
This is the HTML that I need to embed in a page to make this work…
<label for="lastWonDate">Last Jackpot Won Date:</label>
<input type="date" id="lastWonDate" value="2024-11-03">
<button onclick="calculateJackpot()">Calculate</button>
<h2>Results</h2>
<div id="results"></div>
<script src="https://www.lickylip.net/bingo-calculator/script.js"></script>
Test
Documentation Links
Log
[[2024-12-07]]
I tried doing this as an apps script web app but that wasn’t doing what I wanted. So reverted to just doing a JavaScript. This is the first time I’ve one like this. So going to publish this and try and do more js projects in future.