From d58bef13a66f23a65fed5b9e4f1c0003e9c61538 Mon Sep 17 00:00:00 2001 From: Smig Tech Date: Tue, 3 Oct 2023 14:05:11 -0400 Subject: [PATCH] feat: basic js complete --- calculator/js/script.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 calculator/js/script.js diff --git a/calculator/js/script.js b/calculator/js/script.js new file mode 100644 index 0000000..ad18737 --- /dev/null +++ b/calculator/js/script.js @@ -0,0 +1,37 @@ +let firstNum = undefined +let secondNum = undefined +let operator = undefined + +function add(x, y) { + return Number(x + y); +} + +function subtract(x, y) { + return x - y; +} + +function multiply(x, y) { + return x * y; +} + +function divive(x, y) { + if (x === 0) { + return 0 + } else if (y === 0) { + alert("Cannot divide by 0!"); + } + return x / y; +} + +function operate(firstNum, secondNum, operator) { + switch (operator) { + case '+': + return add(firstNum, secondNum); + case '-': + return subtract(firstNum, secondNum); + case '*': + return multiply(firstNum, secondNum); + case '/': + return divive(firstNum, secondNum); + } +}