Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda