Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

252
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda