function change() { var input1 = document.getElementById("input1").value, input2 = document.getElementById("input2").value, sum = 50; if (input1 < 0){ document.getElementById('input1').classList.add('error-input'); } else if (input2 < 0){ document.getElementById('input2').classList.add('error-input'); } else{ document.getElementById("summ").value = (input1 * input2)* sum; document.getElementById('input1').classList.remove('error-input'); document.getElementById('input2').classList.remove('error-input'); } } 
  • A daw can be put only on one answer. - AK
  • and these are the same answers with @Yuri, no matter which one you choose - AK
  • Thank. Got it) - Beksultan Nurkulov
  • Here's another question. And what is the summ, which you multiply? I looked at my code one more time - I got a double impression: you have no summ at all (throw out), it’s some kind of multiplying factor. Explain. - AK
  • Summ is the Id for input where I see the answer of the product, and sum is the constant number - Beksultan Nurkulov

3 answers 3

The code is not bad, but the only thing I would do:

 function change() { var i1 = document.getElementById("input1"), i2 = document.getElementById("input2"), sum = 50; if (i1.value < 0){ i1.classList.add('error-input'); }else if (i2.value < 0){ i2.classList.add('error-input'); }else{ document.getElementById("summ").value = (i1.value * i2.value)* sum; i1.classList.remove('error-input'); i2.classList.remove('error-input'); } } 
  • Thank! Great site)))) - Beksultan Nurkulov
  • @BeksultanNurkulov if you like the answer, increase the counter on the left of the answer, and if you consider it correct, mark it as the correct answer. - Sublihim
  • The code may not be the most effective, but working. Now - the code with errors. - Igor
  • @Igor and I had exactly the same error: did not notice in the summation. )) - AK
  • No, everything is correct. I have. Thanks, it seems there was a mistake in i1.value - Beksultan Nurkulov

Well, hold on. The shortest thing I could think of. Please note that all your code fit into one line, literally in a few words.

 <!doctype html> <html lang="en" ng-app> <head> <meta charset="utf-8"> <title>My HTML File</title> <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js'></script> </head> <body> <input type="text" ng-model='input1' placeholder='input1'> <input type="text" ng-model='input2' placeholder='input2'> <p>input1*input2*50 = {{ input2 ? +input1*+input2*50 : input1 ? 'Введите число в input2!' : 'Введите число в input1!'}}</p> </body> </html> 

  • one
    You can fill in input2 and leave input1 empty, your one-liner does not work :) Although it is probably easy to repair - andreymal

For example:

 function change() { var input1 = document.getElementById("input1"), input2 = document.getElementById("input2"), sum = 50; if (input1.value < 0) { input1.classList.add('error-input'); } else if (input2.value < 0) { input2.classList.add('error-input'); } else { document.getElementById("summ").value = (input1.value * input2.value) * sum; input1.classList.remove('error-input'); input2.classList.remove('error-input'); } } 

Look, you use the same type of constructions like document.getElementById("input1") - and this can be put into variables - the readability of the code improves, because strings are shorter and easier to understand.

For the rest, as for me there is nothing much to improve. Unless to drastically change the script, breaking it up into separate functions, something like:

 function change() { var input1 = document.getElementById("input1"); var input2 = document.getElementById("input2"); var koeff = 50; check_input(input1); check_input(input2); if (input1.value > 0 && input2.value > 0) { document.getElementById("summ").value = (input1.value * input2.value) * koeff; } } function check_input(input) { if (input.value < 0) { input.classList.add('error-input'); } else { input.classList.remove('error-input'); } } 

PS It is not clear that for the multiplication of 50 goes. If this is some kind of coefficient - it should be called so. If it is not needed - then it should be completely cut.

  • Thank you very much! Helped out!)) - Beksultan Nurkulov