Task:
There is a simple calculator that adds two numbers. I want to do to not only add, but also to multiply, subtract, divide, even divide by module. When entering in the field
<input type="button" name="symbol" value="">
operator symbol (*, -, +, /,%) performed arithmetic. I suppose there is some kind of closure here.
Please describe in detail, please, why is it advisable to use closure in this example, or vice versa, perhaps there are other options ??
I added a new input lower in the source, compared to the previous link
Source:
<div class="container"> <input type="text" name="num1" value=""> <input type="button" name="num2" value="="> <input type="button" name="symbol" value=""> <--!добавленный ТЭГ--> <input type="button" name="calc" value="="> <span class="result"></span> </div> let btn = document.querySelector('input[name=calc]'); let num1 = document.querySelector('input[name=num1]'); let num2 = document.querySelector('input[name=num2]'); let result = document.querySelector('.result'); let symbol = document.querySelector('.symbol'); //новое выражение btn.addEventListener('click', totalSum); function totalSum() { }
if(sym=='+') { x=a+b } else if(sym=='-') { x=ab } ...nothing will happen - Mike