Created
November 22, 2024 02:08
-
-
Save thejordanprice/763962997a1bb84f32f1f113254fa2c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let display; | |
let previous = null; | |
let operator = null; | |
let operatorClicked = false; | |
let isFoot = false; | |
let isInch = false; | |
/** | |
* Calculates the operation and updates the display. | |
*/ | |
function performOperation() { | |
if (!previous || !operator) return; // Ignore if no operation | |
let result; | |
const current = parseNumber(feetToInchDecimal(display.value)); | |
const prev = parseNumber(previous); | |
switch (operator) { | |
case '+': | |
result = prev + current; | |
break; | |
case '-': | |
result = prev - current; | |
break; | |
case '*': | |
result = prev * current; | |
break; | |
case '/': | |
if (current === 0) { | |
display.value = "Error"; | |
return; | |
} | |
result = prev / current; | |
break; | |
default: | |
return; | |
} | |
display.value = InchDecimalToFeet(result); | |
previous = null; | |
operator = null; | |
} | |
/** | |
* Parses the display value into a number (float or int). | |
* @param {String} num | |
*/ | |
function parseNumber(num) { | |
return num.includes('.') ? parseFloat(num) : parseInt(num); | |
} | |
/** | |
* Captures the previous value and the clicked operator | |
* so that an operation can be performed. | |
*/ | |
function clickOperator(event) { | |
if (display.value === "") return; // Ignore if no input | |
operator = event.target.value; | |
previous = feetToInchDecimal(display.value); | |
operatorClicked = true; | |
} | |
/** | |
* Captures a number click and updates the display value. | |
* @param {Event} event | |
*/ | |
function clickNumber(event) { | |
const val = event.target.value; | |
if (operatorClicked) { | |
display.value = val; | |
operatorClicked = false; | |
} else { | |
if (val === "'" && !isFoot) { | |
isFoot = true; | |
} else if (val === '"' && !isInch) { | |
isInch = true; | |
} else if (val === '.' && display.value.includes('.')) { | |
return; // Prevent multiple decimal points | |
} | |
display.value = display.value === "0" ? val : display.value + val; | |
} | |
} | |
/** | |
* Resets the display value and state. | |
*/ | |
function clear() { | |
display.value = 0; | |
previous = null; | |
operator = null; | |
operatorClicked = false; | |
isFoot = false; | |
isInch = false; | |
} | |
/** | |
* Converts feet and inches to decimal inches. | |
*/ | |
function feetToInchDecimal(display) { | |
if (isFoot || isInch) { | |
const feetIndex = display.indexOf("'"); | |
const inchIndex = display.indexOf('"'); | |
let footToInch = 0; | |
let inches = 0; | |
if (feetIndex !== -1) { | |
footToInch = parseFloat(display.substring(0, feetIndex)) * 12 || 0; | |
} | |
if (inchIndex !== -1) { | |
inches = parseFloat(display.substring(feetIndex + 1, inchIndex)) || 0; | |
} | |
return (footToInch + inches).toString(); | |
} | |
return display; | |
} | |
/** | |
* Converts decimal inches to feet and inches. | |
*/ | |
function InchDecimalToFeet(results) { | |
if (isFoot || isInch) { | |
const feet = Math.floor(results / 12); | |
const inches = Math.round(results % 12); | |
return `${feet}'${inches}"`; | |
} | |
return results; | |
} | |
// Add event listener for when the DOM is loaded | |
document.addEventListener('DOMContentLoaded', () => { | |
display = document.getElementById('display'); | |
// Number buttons | |
const nums = document.querySelectorAll('.number'); | |
for (const num of nums) { | |
num.addEventListener('click', clickNumber); | |
} | |
// Decimal, foot, and inch buttons | |
const decimalPoint = document.querySelector('.decimal'); | |
decimalPoint.addEventListener('click', clickNumber); | |
const foot = document.querySelector('.foot'); | |
foot.addEventListener('click', clickNumber); | |
const inch = document.querySelector('.inch'); | |
inch.addEventListener('click', clickNumber); | |
// Clear button | |
const clearButton = document.querySelector('.all-clear'); | |
clearButton.addEventListener('click', clear); | |
// Operator buttons | |
const operators = document.querySelectorAll('.operator'); | |
for (const operator of operators) { | |
operator.addEventListener('click', clickOperator); | |
} | |
// Equal sign | |
const equal = document.querySelector('.equal-sign'); | |
equal.addEventListener('click', performOperation); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment