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

Time:02-04

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>

CodePudding user response:

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>

CodePudding user response:

The following should work if your HTML code contains an element with the property id="fkWynik"

function mat_WypiszLiczbyNaturalne() {
  var elem = document.getElementById('fkEdit');
  if(!elem) {
    console.error("No element with id 'fkEdit' in the page.");
    return;
  }

  var T = elem.value;
  var value = "";

  if (T && !isNaN(Number(T))) { // if T not null, not empty, not undefined, not 0 and is a number
    var S = "";
    for (var I = 1; I < Number(T)   1; I  ) {
      S = S   ", "   I.toString();
    }
    value = S.substr(2)   " = ";
  } else {
    value = "Proszę wprowadzić liczbę!";
  }

  console.log("value: "   value);  
  document.getElementById('fkWynik').value = value;
}

mat_WypiszLiczbyNaturalne();
  •  Tags:  
  • Related