Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
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>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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();
about 4 years ago · Juan Pablo Isaza Report

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!