I need help with this javascript and html cd, there is no return of values from "Function Calc(x)" to "Function total(num1, num2, X)"

Asked

Viewed 24 times

-1

What was the mistake? I was doing a calculator project where I used "onclick" and each button gave a number from 1 to 4. In the method "Total(num1, num2, X)" I used parameter-by-position, the result should go back to INPUT TYPE "TEXT", but there is no return of values. I don’t know what my mistake was.

    function calc(X)
    {
      var num1 = document.getElementById("nmr1").value;
      var num2 = document.getElementByid("nmr2").value;

      if (isNaN(num1) || (num1=="")) 
      {
       alert("Você sabe ler? Digite um n°");
       document.getElementById("nmr1").value = "";
       document.getElementById("nmr1").focus();
      }
      else if(isNaN(num2) || (num2==""))
      {
       window.alert("Você sabe ler? Digite um n°");
       document.getElementById("nmr2").value = "";
      }
      else
      {
       num1 = eval(num1);
       num2 = eval(num2);
       document.getElementById("resu").value = Total(num1, num2, X);
      }
      
    }
    function Total(num1, num2, X)
    {
      var resultado;

       if(X==1)
       resultado = num1 + num2;
       else if(X==2)
       resultado = num1 - num2;
       else if(X==3)
       resultado = num1 * num2;
       else if(X==0)
       {
         window.alert("Não existe divisão por 0");
         resultado = "Não há valor válido";
         document.getElementById("resu").size=14;
       }
       else 
      
        resultado = num1/num2;
      
      return resultado;
    }
                function aumenta(X)
        {
          if(X==1)
          document.getElementById("nmr1").size=8;
          else
          document.getElementById("nmr2").size=8;  
        }
        function diminui(X)
        {
          if(X==1)    
          document.getElementById("nmr1").size=2;            
          else           
          document.getElementById("nmr2").size=2;               
        }
        function limpar()
        {
          document.getElementById("nmr1").focus(); 
        }
     </SCRIPT> 
</HEAD>
<BODY>
  <blockquote><blockquote><blockquote><blockquote><blockquote><blockquote><blockquote><blockquote></blockquote>
   <H5> 
    <FORM>
      <H3><CENTER>Calculadora Doida</CENTER></H3>
      <HR/>
      &nbsp;&nbsp;&nbsp;&nbsp;INSIRA O 1° VALOR  
      <INPUT Type = "text" id = "nmr1" onFocus = "aumenta(1)" onBlur = "diminui(1)"/> <br/><br/>
      &nbsp;&nbsp;&nbsp;&nbsp;INSIRA O 2° VALOR 
      <INPUT Type = "text" id = "nmr2" onFocus = "aumenta(2)" onBlur = "diminui(2)"/> <br/><br/>
      &nbsp;&nbsp;&nbsp;&nbsp;O RESULTADO É
      <INPUT Type = "text" id = "resu" onChange = "aumenta(3)" disabled/> <br/><br/><br/> 
      <!--value: o valor que vai escrito no item/objeto-->
      <INPUT Type = "button" value = "+" onClick = "calc(1)" class = "pressiona"/>
      &nbsp;&nbsp;
      <INPUT Type = "button" value = "-" onClick = "calc(2)" class = "pressiona"/>
      &nbsp;&nbsp;
      <INPUT Type = "button" value = "x" onClick = "calc(3)" class = "pressiona"/>
      &nbsp;&nbsp;
      <INPUT Type = "button" value = ":" onClick = "calc(4)" class = "pressiona"/>
      &nbsp;&nbsp;
      <INPUT Type = "reset" value  = "C" onClick = "limpar()" class = "pressiona"/>
      <BR/><BR/><BR/>
      <HR/>
      <br/><br/><br/>
    </FORM>  
   </H5>
  </blockquote></blockquote></blockquote></blockquote></blockquote></blockquote> </blockquote></blockquote>            
</BODY>

1 answer

0


Hello.

Every WEB developer must learn to use the browser console. Just press F12 and look at the console to see that it is giving error in your script:

Uncaught TypeError: document.getElementByid is not a function

It happened because the method getElementsById is with the tiny "i"

Tips

Avoid using var, use let instead. You can also use querySelector instead of using getElementsById.

let num1 = document.querySelector("#nmr1").value;

https://blog.schoolofnet.com/differca-entre-var-e-let-no-javascript/#:~:text=When%20voc%C3%AA%20declares%20uma%20vari%C3%A1vel,global,%20local%20and%20de%20block.

  • thank you very much, friend. the problem was obvious and I did not notice it. Thank you very much, have a good day

  • I added a few tips to the answer =)

  • thanks again, I will use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.