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

189
Views
Return the Value of a random array

So I am trying a simple function within HTML that will change the styling to a random color from an array. I've successfully randomized it, but it only returns the index value and not the index string value. The console thinks I want 3 rather than "radial-gradient(#8FA19E, #0A917B)". How can I change it to the right value?

const colorBtn = document.querySelector("button")    

function changeColor(){    
    let colors = [
        "radial-gradient(#DE0404, #EBDFDF)",
        "radial-gradient(#CDD663, #050504)",
        "linear-gradient(#B4C2B2, #092B04)",
        "radial-gradient(#8FA19E, #0A917B)",
        "linear-gradient(#161617, #2A07DB)",
        "radial-gradient(#524D51, #FF00CC)",
    ]

    const randomColor = Math.floor(Math.random() * colors.length)
    document.querySelector("body").style.background = `${randomColor}`
    console.log(`${randomColor}`)

}

colorBtn.addEventListener('click', changeColor)    
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const randomColor = Math.floor(Math.random() * colors.length)
document.querySelector("body").style.background = `${colors[randomColor]}`
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!