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

143
Views
Stuck on background color change

I tried this project from a little while ago and even copy and pasted some parts from the working projects I had saved. The background color wont change and I am confused.

const colors = ['red', 'green', 'blue'];

const colorIndex = -1;

let myFunction = () => {
    colorIndex += 1;
    if (colorIndex > colors.length-1) colorIndex = 0;
    document.body.style.backgroundColor = colors[colorIndex];
}
body{
    text-align: center;
}
#btn {
    margin-top: 25%;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Colour Flipper</title>
    <link rel="stylesheet" href="color.css">
</head>
<body>
    <button id="btn" onclick="myFunction()">Click to change colour</button>
</body>
<script src="color.js"></script>
</html>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Uncaught TypeError: Assignment to constant variable. Change constant variable

const colorIndex = -1;

to

let colorIndex = -1;
about 4 years ago · Juan Pablo Isaza Report

0

Use let instead of const. You cannot reassign const.

const colors = ['red', 'green', 'blue'];

let colorIndex = -1;

let myFunction = () => {
    colorIndex += 1;
    if (colorIndex > colors.length-1) colorIndex = 0;
    document.body.style.backgroundColor = colors[colorIndex];
}

about 4 years ago · Juan Pablo Isaza Report

0

This is the Javascript. You initialise the variables with const. But you have to do it with let or var.

let colors = ['red', 'green', 'blue'];

let colorIndex = -1;

let myFunction = () => { 
    colorIndex += 1;
    if (colorIndex > colors.length-1) colorIndex = 0;
    document.body.style.backgroundColor = colors[colorIndex];
}
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!