The if loop only works if there is an alert in it .. just by removing the alert ("LoL") it stops working, is it a bug or a bug? the function overwrites the operation sign on the calculator

function changeAction(value) { var input = document.getElementById("t"); if (input.value.length == 2) { alert("LoL"); if (input.value =='++' || input.value == '-+' || input.value =='/+' || input.value =='*+') { input.value = '+'; } if (input.value =='--' || input.value == '/-' || input.value =='*-' || input.value =='+-') { input.value = '-'; } if (input.value =='-*' || input.value == '/*' || input.value =='**' || input.value =='+*') { input.value = '*'; } if (input.value =='//' || input.value == '*/' || input.value =='*-/' || input.value =='+/') { input.value = '/'; } } } 
  • one
    if it's not a loop, where does the assumption that it stops working? how exactly does not work? How is changeAction called? - Grundy
  • In general, this whole if can be replaced by something like this: if (input.value) input.value = input.value.charAr(input.value.length - 1); - Yaant
  • @Yaant, you are wrong, and there is a typo in the name of the function, which you can no longer use as you can refer to characters by input.value[index] - Grundy for a long time
  • input.value =='*-/' - here the length is not 2. - Qwertiy

1 answer 1

First, if is a conditional operator, not a loop. Secondly, the alert only suspends execution, but it does not affect the operation of this code.

Try searching for an error using debugger (F12).

  • And if you use the debugger keyword to set up bryakpoint, debugging will become even easier. - Evgeny Borisov
  • @YevgenyBorisov, debugger - changes the code, and to make it conditional - you need to change the code again - Grundy
  • @Grundy is only about stopping method. Any self-respecting javascript engine encountering the debugger keyword will stop execution if the debugger is active. - Evgeny Borisov
  • @YevgenyBorisov, in order for him to meet him, he must be written in the code , which means changing the source code. If you need a conditional stopping point, add a line with the condition, more source code changes - Grundy