|
| 1 | +function classifyTriangle(a, b, c) { |
| 2 | + a = Number(a); |
| 3 | + b = Number(b); |
| 4 | + c = Number(c); |
| 5 | + |
| 6 | + if (a <= 0 || b <= 0 || c <= 0 || a + b <= c || a + c <= b || b + c <= a) { |
| 7 | + return "not-a-triangle"; |
| 8 | + } else if (a === b && b === c) { |
| 9 | + return "equilateral"; |
| 10 | + } else if (a === b || b === c || a === c) { |
| 11 | + return "isosceles"; |
| 12 | + } else { |
| 13 | + return "scalene"; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +document.getElementById("triangleForm").addEventListener("submit", function (event) { |
| 18 | + event.preventDefault(); |
| 19 | + |
| 20 | + const a = document.getElementById("a").value; |
| 21 | + const b = document.getElementById("b").value; |
| 22 | + const c = document.getElementById("c").value; |
| 23 | + |
| 24 | + const resultType = classifyTriangle(a, b, c); |
| 25 | + const resultText = `Result: ${resultType.replace(/-/g, " ")}`; |
| 26 | + |
| 27 | + // Update URL and show result |
| 28 | + history.pushState({ a, b, c }, "", `/${resultType}`); |
| 29 | + const resultEl = document.getElementById("result"); |
| 30 | + resultEl.textContent = resultText; |
| 31 | + resultEl.classList.remove("hidden"); |
| 32 | +}); |
| 33 | + |
| 34 | +// Handle back/forward navigation |
| 35 | +window.onpopstate = function () { |
| 36 | + const resultEl = document.getElementById("result"); |
| 37 | + if (location.pathname === "/") { |
| 38 | + resultEl.classList.add("hidden"); |
| 39 | + } else { |
| 40 | + const text = location.pathname.slice(1).replace(/-/g, " "); |
| 41 | + resultEl.textContent = `Result: ${text}`; |
| 42 | + resultEl.classList.remove("hidden"); |
| 43 | + } |
| 44 | +}; |
0 commit comments