var selection = []; $(document).ready(function () { $(".calc-button").on("click", function () { var curr = $(this).text(); if (curr !== "AC" && curr !== "=" && curr !== "DEL") { selection.push(curr); updateScreen(); } else if (curr === "AC") { selection = []; $("#screen").text("0"); } else if (curr === "DEL") { selection.pop(); if (selection.length === 0) { $("#screen").text("0"); } else { updateScreen(); } } else if (curr === "=") { showAnswer(); } }); }); function updateScreen() { $("#screen").text(selection.join("")); } function showAnswer() { selection = selection.map(function (curr, index) { if (curr === "÷") { return "/"; } else if (curr === "×") { return "*"; } else { return curr; } }); var answer; try { answer = eval(selection.join("")); } catch (e) { if (e instanceof SyntaxError) { answer = "Math error"; } } selection = []; answer = (answer + "").replace("Infinity", "∞"); answer = (answer + "").replace("NaN", "Error 404, logic not found"); $("#screen").text(answer); }