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

98
Visualizações
Im learning JS and i have a task to make the numbers that i input reverse and pop up in an alert

i made the script that reverses the numbers but i dont know how to make the alert pop up the result of the reversed numbers I need help to figure this out it probably has a simple solution but i dont know

The code added to snippet is below:

function okreni () { // removed "s" parameter
  var a = ' ';
  // s = s.toString();
  const s = document.getElementById("broj").value.toString();
  for (var i = s.length - 1; i>=0; i--) {
    a += s[i];
  }
  window.alert (a);
};
<body>
  <label for="broj">Unesite Broj:</label>
  <input type="number" name="broj" id="broj" value="">
  <div>
    <button  value="okreni" onclick="okreni()">Okreni</button>
  </div>
</body>

EDIT -

The s = s.toString() has been changed to get the information from the input-value.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

alert doesn't display if there's no value to display. in your case you have to passe a value to "okreni()" function.

<button  value="okreni" onclick="okreni(**value**)">Okreni</button>
about 4 years ago · Juan Pablo Isaza Relatório

0

Apparently, you suppose to get the input value as s in okreni(s). However, this is not possible. You have to get the value programatically from the input. Following the working code. I've also created this CodeSandbox for you to try it out:

<!DOCTYPE html>
<html>
  <head>`enter code here`
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
  </head>
  <body>
    <label for="broj">Unesite Broj:</label>
    <input type="number" name="broj" id="broj" value="" />
    <div>
      <button value="okreni" onclick="okreni()">Okreni</button>
    </div>
    <script type="text/javascript">
      function okreni() {
        var a = " ";
        let inputValue = document.querySelector("#broj").value;
        const s = inputValue.toString();
        for (var i = s.length - 1; i >= 0; i--) {
          a += s[i];
        }
        window.alert(a);
      }
    </script>
  </body>
</html>
about 4 years ago · Juan Pablo Isaza Relatório

0

You could also try something like this to reverse your string. In looks much cleaner in my opinion and can even be condensed to a single line if needed.

Apart from that, the reason you are getting an error is because of what alexanderdavide mentioned in his answer. To elaborate further, the okreni function does not require a parameter to be passed. Instead, within the fucntion we look for the value in the input element with the id of broj. So, when you click on the button, the function checks the string in that input, reverses it and then performs an alert.

function okreni() {
  let s = document.getElementById('broj').value
  s = s.split("").reverse().join("")
  window.alert(s)
}
<label for="broj">Unesite Broj:</label>
<input type="text" name="broj" id="broj" value="">
<div>
  <button value="okreni" onclick="okreni()">Okreni</button>
</div>

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