я написал этот код: function backspace() { if (exp.length = 1) exp = "0" else exp =…
- User
я написал этот код: function backspace() { if (exp.length = 1) exp = "0" else exp = exp.substring(0, exp.length - 1) result.innerHTML = exp } exp равен "100" но когда срабатывает эта функция exp становиться "0"
- Assistant
Проблема в вашем коде вызвана неправильным использованием оператора сравнения. Вместо оператора сравнения (
==или===), вы используете оператор присваивания (=). В результате этого вы меняете значение переменнойexpна "0" вместо проверки, равно ли значениеexp1.Исправьте это, заменив оператор присваивания на оператор сравнения в строке
if (exp.length = 1). Измените его наif (exp.length === 1). Таким образом, ваш исправленный код будет выглядеть следующим образом:function backspace() { if (exp.length === 1) exp = "0"; else exp = exp.substring(0, exp.length - 1); result.innerHTML = exp; }Теперь код должен работать правильно и значение
expне будет изменяться на "0", если его длина не равна 1. - User
var exp = '' const result = document.getElementById('result') const keys = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', '-', '/', '*', '.']
function error() { result.innerHTML = 'error' exp = '' }
function btnClick(txt) { if (txt == '÷') txt = '/' if (txt == '×') txt = '*' exp += txt result.innerHTML = exp }
function count() { var res try { res = eval(exp) } catch { error() return } if (res == 'Infinity') { error() return } result.innerHTML = res exp = res }
function clearall() { exp = '' result.innerHTML = '0' }
function backspace() { if (exp.length < 2) exp = "0" else exp = exp.substring(0, exp.length - 1) result.innerHTML = exp }
window.onkeydown = (e) => { if (e.key in keys) btnClick(e.key) } but when i press /, *, + and - nothing happened but when i click any of numbers it works
- Assistant
The reason why pressing
/,*,+, and-does not do anything is because these keys are not present in thekeysarray. ThebtnClickfunction only executes its logic if the pressed key (txt) is present in thekeysarray.To fix this issue, you need to add
/,*,+, and-to thekeysarray. Update the line:const keys = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', '-', '/', '*']After making this change, pressing
/,*,+, or-should execute thebtnClickfunction and add the corresponding operator to theexpvariable.Additionally, there is one more mistake in your code. In the
countfunction, you are comparingres(which is the result of the evaluation) with the string using the equality operator (). This will not work as expected because is a string, and the result of dividing by zero is a number (). - User
but this doesn't helps
- Assistant
I apologize for the confusion. Upon further examination of your code, it seems that there are a couple of issues that are causing the operators (
/,*,+,-) to not work as expected. Here are the corrections you need to make:- In the
btnClickfunction, update the conditional statements:
function btnClick(txt) { if (txt === '÷') txt = '/'; if (txt === '×') txt = '*'; exp += txt; result.innerHTML = exp; }The comparison operators (
==) have been changed to strict equality operators (===).- The second issue is with the
window.onkeydownevent listener. Changee.key in keystokeys.includes(e.key)to check if the pressed key is present in thekeysarray:
window.onkeydown = (e) => { if (keys.includes(e.key)) btnClick(e.key); }This change will allow the
/,*,+, and-keys to be recognized by the event listener. - In the