Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Js How do I add up all numbers into one variable and show it?

Whenever I try doing the code myself, nothing shows up after I run the function, I’m wanting to add all the numbers from ‘fkWynik’.

function mat_WypiszLiczbyNaturalne() {
  var T = "";
  T = document.getElementById('fkEdit').value;
  if ((T.trim() != "") && (Number(T) > 0)) {
    var S = "";
    for (var I = 1; I < Number(T) + 1; I++) {
      S = S + ", " + I.toString();
    }
    document.getElementById('fkWynik').value = S.substr(2) + " = ";

  } else {
    document.getElementById('fkWynik').value = "Prosze wprowadzic liczbe!";
  }
}
<html>
<body>
   <FORM NAME="formularz1" ACTION=""> 
     <TABLE BORDER="0"> 
                    &nbsp;<INPUT TYPE="number" ID="fkEdit" STYLE="height:24px; width:55px;"> 
                    &nbsp;<INPUT TYPE="button" ID="fkWykonaj" VALUE="Wykonaj" onClick="mat_WypiszLiczbyNaturalne();">&nbsp;</TD> 
            </TR> 
            <TR><TD>&nbsp;<INPUT TYPE="text" ID="fkWynik" STYLE="width:545px; height:24px;" READONLY>&nbsp;</TD></TR> 
     </TABLE> 
   </FORM> 
</body>
</html>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need to add the numbers together and concat it to the string:

    function mat_WypiszLiczbyNaturalne() {
      var T = "";
      T = document.getElementById('fkEdit').value;
      if ((T.trim() != "") && (Number(T) > 0)) {
        var S = "";
        var total = 0 
        for (var I = 1; I < Number(T) + 1; I++) {
          S = S + ", " + I.toString();
          total += I;
        }
        document.getElementById('fkWynik').value = S.substr(2) + " = "+total.toString();
    
      } else {
        document.getElementById('fkWynik').value = "Prosze wprowadzic liczbe!";
      }
    }
<html>
<body>
   <FORM NAME="formularz1" ACTION=""> 
     <TABLE BORDER="0"> 
                    &nbsp;<INPUT TYPE="number" ID="fkEdit" STYLE="height:24px; width:55px;"> 
                    &nbsp;<INPUT TYPE="button" ID="fkWykonaj" VALUE="Wykonaj" onClick="mat_WypiszLiczbyNaturalne();">&nbsp;</TD> 
            </TR> 
            <TR><TD>&nbsp;<INPUT TYPE="text" ID="fkWynik" STYLE="width:545px; height:24px;" READONLY>&nbsp;</TD></TR> 
     </TABLE> 
   </FORM> 
</body>
</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading