Why don't js functions work?
-
I need to create a kind of calculator with a list of fields like this:
Number 1 - one line field
Number 2 - one line field
Function - drop-down list with values (sum, sub, div, mul, pow, sqrt)
The result is a one-line field.
When you press the button, the validity of the entered values for the selected arithmetic operation is checked. In case of an input error, display a message. Then perform the operation and compare with the entered value in the "Result" field. If it doesn't match, issue an error message.
If everything is successful, go to another page. I go to another page anyway.
Visually, the form looks like this (for a better understanding of the problem):
<form style="margin: 0 100px" action="index2.html" onsubmit="return toSecondPage()"> <p>Первое число:<input id = "number1" required><span id="warningMessage"></span></p> <p>Второе число:<input id = "number2" required><span class="warningMessage"></span></p> <p> Действие: <select id="action"> <option value="sum">sum</option> <option value="sub">sub</option> <option value="mul">mul</option> <option value="pow">pow</option> <option value="div">div</option> <option value="sqrt">sqrt</option> </select> </p> <p>Ваш результат:<input id = "userResult" required><span class="warningMessage"></span></p> <input type="submit" value="Check result"> </form>
let warning = document.getElementsByClassName("warningMessage") let act = document.getElementById('action').value; let num1 = Number(document.getElementById('number1').value); let num2 = Number(document.getElementById('number2').value); let userRes = Number(document.getElementById('userResult').value); let trueRes = 0; //Проверяю вернули ли все функции 'true', если да - перехожу на другую станицу function toSecondPage(){ if (checkNumbers() && action() && CheckResult()) { return true; } else { return false; } } //Проверяю заполнены ли поля числами, Почему-то когда я ввожу строку - программа все равно воспринимает как тип number function checkNumbers(){ if(isNaN(num1) || isNaN(num2) || isNaN(userRes)){ warning[0].innerHTML = "Поле должно быть заполнено числом!" warning[1].innerHTML = "Поле должно быть заполнено числом!" warning[2].innerHTML = "Поле должно быть заполнено числом!" return false; } else{ return true; } } //Выполняю арифметические действия function action(){ if(act == "sum"){ trueRes = num1 + num2 return true; } else if(act == "sub"){ trueRes = num1 - num2 return true; } else if(act == "mul"){ trueRes = num1 * num2 return true; } else if(act == "sum"){ trueRes = Math.pow(num1, num2) return true; } else if(act == "div"){ if(num2 === 0){ warning[1].innerHTML = "Ошибка, деление на ноль!" return false; } else{ trueRes = num1 / num2 return true; } } else if(act == "sqrt"){ if(num1 <= 0){ warning[0].innerHTML = "Число должно быть больше 0!" return false; } else{ trueRes = Math.sqrt(num1); return true; } } } //Проверяю сходится ли истинный результат с результатом пользователя function CheckResult(){ if(userRes !== trueRes){ warning[0].innerHTML = `Вы ошиблись, правильный результат: ${trueRes}` return false; } else{ return true; } }
JavaScript Anonymous, Jul 2, 2020 -
You read the values from the fields 1 time at the start of the program, but you need to read it for each button pressAnonymous
1 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!