Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const result = document.querySelector(".result");
- const btns = document.querySelectorAll(".btns");
- let num1 = null;
- let oper = null;
- btns.forEach(function(btns) {
- btns.addEventListener("click", function(){
- if(num1 == null) {
- if (btns.id == "btn0" && result.firstChild.nodeValue != ' '){
- result.firstChild.nodeValue += 0;
- }
- for(let i = 1; i<10; i++){
- if (btns.id == "btn" + i) {
- result.firstChild.nodeValue += i;
- }
- }
- }
- if(num1 != null) {
- if (btns.id == "btn0" && result.firstChild.nodeValue != ' '){
- result.firstChild.nodeValue += 0;
- }
- for(let i = 1; i<10; i++){
- if (btns.id == "btn" + i) {
- result.firstChild.nodeValue += i;
- }
- }
- }
- });
- });
- const calc = document.querySelectorAll(".calc");
- calc.forEach(function(calc) {
- calc.addEventListener("click", function(){
- if (!(isNaN(result.firstChild.nodeValue))){
- if (calc.id == "btnplus") {
- num1 = result.firstChild.nodeValue;
- if (result.firstChild.nodeValue == ' ') {
- result.firstChild.nodeValue = "+";
- }
- else {
- result.firstChild.nodeValue = " ";
- }
- oper = "+";
- }
- if (calc.id == "btnminus") {
- num1 = result.firstChild.nodeValue;
- if (result.firstChild.nodeValue == ' ') {
- result.firstChild.nodeValue = "-";
- }
- else {result.firstChild.nodeValue = " ";
- }
- oper = "-";
- }
- if (calc.id == "btnmultiple") {
- if (result.firstChild.nodeValue != ' '){
- num1 = result.firstChild.nodeValue;
- result.firstChild.nodeValue = " ";
- oper = "*";
- }
- }
- if (calc.id == "btndivide") {
- if (result.firstChild.nodeValue != ' '){
- num1 = result.firstChild.nodeValue;
- result.firstChild.nodeValue = " ";
- oper = ":";
- }
- }
- }
- });
- });
- const equals = document.querySelector(".equals");
- equals.addEventListener("click", function(){
- if (num1 != null && oper != null && result.firstChild.nodeValue != ' ') {
- switch(oper){
- case "+":
- result.firstChild.nodeValue = parseInt(num1) + parseInt(result.firstChild.nodeValue);
- num1, oper = null;
- break;
- case "-":
- result.firstChild.nodeValue = parseInt(num1) - parseInt(result.firstChild.nodeValue);
- num1, oper = null;
- break;
- case "*":
- result.firstChild.nodeValue = parseInt(num1) * parseInt(result.firstChild.nodeValue);
- num1, oper = null;
- break;
- case ":":
- result.firstChild.nodeValue = parseInt(num1) / parseInt(result.firstChild.nodeValue);
- num1, oper = null;
- break;
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement