This question has already been answered:

The function displays the last element, but I don’t know how to conduct the correct zero check, more precisely, how I did it doesn’t work. What is the problem?

function div(input) { var input = document.getElementById("t"); var lastElement = (input.value.length-1); alert(input.value.charAt(lastElement));//заглушка для проверки if (input.value.charAt(lastElement) == 0) { alert(" / to Zero"); } } 

Reported as a duplicate by the participants cheops , Grundy , VenZell , Dmitriy Simushev , insolor May 7 '16 at 17:56 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • What can be in input.value? - alias
  • one
    In my opinion already the third question on the same topic, can it be better to correct the previous questions? - cheops

1 answer 1

When designing a calculator, remember the following mathematical rule:
The number over which the manipulation is carried out is called - operated ;
The type of operation is called an operator , for example, "+" or "-" ;
The number that is being manipulated is called the operand ;
So, it is necessary to check the operator and if it is "/", check the operand to zero.
For example:

 var operate = prompt('число',''); var operator = prompt('операция "+", "-","*","/" ',''); var operand = prompt('число',''); var result; switch (operator) { case "+": { result = +operate + +operand; document.write('Резултат' + result); } break; case "-": { result = +operate - +operand; document.write('Резултат' + result); } break; case "*": { result = +operate * +operand; document.write('Резултат' + result); } break; case "/": { if (operand == 0){ document.write('На ноль делить нельзя'); } else{ result = +operate / +operand; document.write('Резултат' + result); } } break; }