|
| 1 | +/** |
| 2 | + * Programmer Math Practice App |
| 3 | + * |
| 4 | + * A tool to help junior developers practice some basic programming math. |
| 5 | + * @author Benjamin P.C. Hovinga |
| 6 | + * @copyright Copyright 2025 Benjamin P.C. Hovinga |
| 7 | + * @license MIT |
| 8 | + * |
| 9 | + * *This program does not contain AI generated code.* |
| 10 | + */ |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +function App() { |
| 15 | + console.clear(); |
| 16 | + console.info('Starting App...') |
| 17 | + |
| 18 | + // Save the pointers for the selection elements. |
| 19 | + const convertFromElements = document.getElementsByName('convertFrom'); |
| 20 | + const convertToElements = document.getElementsByName('convertTo'); |
| 21 | + |
| 22 | + // Set the default states of the app. |
| 23 | + const conversionOptions = ['dec', 'bin', 'hex']; |
| 24 | + let convertFrom = conversionOptions[0]; |
| 25 | + let convertTo = conversionOptions[1]; |
| 26 | + convertFromElements[0].checked = true; |
| 27 | + convertToElements[0].disabled = true; |
| 28 | + convertToElements[1].checked = true; |
| 29 | + |
| 30 | + // When 'convertFrom' changes. |
| 31 | + document.getElementById('convertFrom').addEventListener('change', function(event) { |
| 32 | + // Update the selected value. |
| 33 | + convertFrom = event.target.value; |
| 34 | + console.info(`User updated 'convertFrom': '${convertFrom}'`); |
| 35 | + |
| 36 | + // Disable the sister option in 'convertTo'. |
| 37 | + convertToElements.forEach(function (element, index) { |
| 38 | + // Reset disabled elements. |
| 39 | + element.disabled = false; |
| 40 | + |
| 41 | + // When we get to the sister option. |
| 42 | + if (element.value === convertFrom) { |
| 43 | + // Disable sister option. |
| 44 | + element.disabled = true; |
| 45 | + |
| 46 | + // Change the selection if both 'to' and 'from' are selected. |
| 47 | + if (convertFrom === convertTo) { |
| 48 | + if (index >= 2) { |
| 49 | + // Move to top position. |
| 50 | + convertToElements[0].checked = true; |
| 51 | + convertTo = conversionOptions[0]; |
| 52 | + } else { |
| 53 | + // Move to next position. |
| 54 | + convertToElements[index + 1]. checked = true; |
| 55 | + convertTo = conversionOptions[index + 1]; |
| 56 | + } |
| 57 | + console.debug(`Program updated 'convertTo': '${convertTo}'`) |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + // When 'Convert To' changes. |
| 64 | + document.getElementById('convertTo').addEventListener('change', function(event) { |
| 65 | + // Update the selected value |
| 66 | + convertTo = event.target.value; |
| 67 | + console.info(`User updated 'convertTo': '${convertTo}'`); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +// Start the app when the DOM is ready. |
| 72 | +window.addEventListener('DOMContentLoaded', App); |
0 commit comments