Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!