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

174
Vistas
Issue trying to make a color flipper with Javascript

Thisis my first JS project and i cant figure out where i went wrong, i am trying to make a color flipper whre if you click a button it picks a random color from an array of colors and make that the background color.

const colors = ["green", "red", "rgba(133,122,200)", "#f15025"];

const button = document.getElementById("btn");
const color = document.querySelector(".color");

button.addEventListener("click", function () {

  const randomItem = colors[getRandomItem];
  document.body.style.backgroundColor = randomItem;
  color.textContent = randomItem;

});

const getRandomItem = function () {
  return Math.floor(Math.random() * colors.length);
}
<body>
    <!-- javascript -->
    <script src="app.js"></script>

    <nav>
        <div class="nav-center">
            <h4>color flipper</h4>
            <ul class="nav-links">
                <li><a href="index.html">simple</a></li>
                <li><a href="hex.html">hex</a></li>
            </ul>
        </div>
    </nav>
    <main>
        <div class="container">
            <h2>background color : <span class="color">#f1f5f8</span></h2>
            <button class="btn btn-hero" id="btn">click me</button>
        </div>
    </main>

</body>

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

0

In this line:

const randomItem = colors[getRandomItem];

you aren't calling the function. Put parentheses after you write its name: getRandomItem().

Snippet

const colors = ["green", "red", "rgba(133,122,200)", "#f15025"];

const button = document.getElementById("btn");
const color = document.querySelector(".color");

button.addEventListener("click", function () {

  const randomItem = colors[getRandomItem()];
  document.body.style.backgroundColor = randomItem;
  color.textContent = randomItem;

});

const getRandomItem = function () {
  return Math.floor(Math.random() * colors.length);
}
<body>
    <!-- javascript -->

    <nav>
        <div class="nav-center">
            <h4>color flipper</h4>
            <ul class="nav-links">
                <li><a href="index.html">simple</a></li>
                <li><a href="hex.html">hex</a></li>
            </ul>
        </div>
    </nav>
    <main>
        <div class="container">
            <h2>background color : <span class="color">#f1f5f8</span></h2>
            <button class="btn btn-hero" id="btn">click me</button>
        </div>
    </main>

</body>

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