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

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

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 Denunciar

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 Denunciar

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