123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- 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);
- }
|