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

133
Vistas
I'm trying to make a test html where pressing a button moves along the index of an array which starts at 0

When I test this code it only shows 0, despite the button function running. What have I done wrong? Since I'm only starting out with JavaScript I'd appreciate an in-depth answer.

var y = 1;
myFunction();

function myFunc() {
  var z = 1
  var x = y + z;
  y = x
  alert("f");
}

function myFunction() {
  y = 0;
  alert("sdrz");
}
const numbers = ["0", "1", "2", "3"];
document.getElementById("demo").innerHTML = numbers[y];
<p id="demo"></p>

<button onclick="myFunc()">+1</button>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Aside from a bunch of other style issues, your innerHTML assignment was not in the onclick, "myFunc" handler, so it was never getting executed. Also your function will exceed the array limits if clicked on too many times.

<!DOCTYPE html>
<html>
  <body >
   <p id="demo"></p>
   <button onclick="myFunc()">+1</button>
   <script>
   const numbers = ["0", "1", "2", "3"];
   var y=0;

   function myFunc() {
     document.getElementById("demo").innerHTML = numbers[y];
     y++;
   }

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