Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

405
Visualizações
How can I make the code generate the colors correctly?

I am creating a website for creating color palettes but I can't seem to solve a non-JavaScript error (probably html or CSS error) that I am facing.

var c1 = document.getElementById('c1');
var c2 = document.getElementById('c2');
var c3 = document.getElementById('c3');
function rand(x) {
  return (Math.random() * x).toFixed(0);
}
function cc() {
  let y = rand(360);
  let z;
  if (rand(2) === 0) {
    z = 50;
  } else if (rand(1) === 1) {
    z = 120;
  } else {
    z = 170;
  }
  c1.style.backgroundColor = `hsl(${y}deg, ${50+rand(50)}%, ${rand(25)+25}%)`;
  c2.style.backgroundColor = `hsl(${(y+z)%360}deg, ${50+rand(50)}%, ${rand(25)+25}%)`;
  c3.style.backgroundColor = `hsl(${(y+z+z)%360}deg, ${50+rand(50)}%, ${rand(25)+25}%)`;
}
cc();
<button onclick='cc();'>Create Color Themes!</button>
<div id='c1'>X</div>
<div id='c2'>X</div>
<div id='c3'>X</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Simple debugging will show you your problem.

var c1 = document.getElementById('c1');
var c2 = document.getElementById('c2');
var c3 = document.getElementById('c3');
function rand(x) {
  return (Math.random() * x).toFixed(0);
}
function cc() {
  let y = rand(360);
  let z;
  if (rand(2) === 0) {
    z = 50;
  } else if (rand(1) === 1) {
    z = 120;
  } else {
    z = 170;
  }
  console.log(`hsl(${y}deg, ${50+rand(50)}%, ${rand(25)+25}%)`)
  c1.style.backgroundColor = `hsl(${y}deg, ${50+rand(50)}%, ${rand(25)+25}%)`;
  c2.style.backgroundColor = `hsl(${(y+z)%360}deg, ${50+rand(50)}%, ${rand(25)+25}%)`;
  c3.style.backgroundColor = `hsl(${(y+z+z)%360}deg, ${50+rand(50)}%, ${rand(25)+25}%)`;
}
cc();
<button onclick='cc();'>Create Color Themes!</button>
<div id='c1'>X</div>
<div id='c2'>X</div>
<div id='c3'>X</div>

You are returning a string. A string plus a number is a string. So you need your random code to return a number.

var c1 = document.getElementById('c1');
var c2 = document.getElementById('c2');
var c3 = document.getElementById('c3');
function rand(x) {
  return Math.floor(Math.random() * x);
}
function cc() {
  let y = rand(360);
  let z;
  if (rand(2) === 0) {
    z = 50;
  } else if (rand(1) === 1) {
    z = 120;
  } else {
    z = 170;
  }

  c1.style.backgroundColor = `hsl(${y}deg, ${50+rand(50)}%, ${rand(25)+25}%)`;
  c2.style.backgroundColor = `hsl(${(y+z)%360}deg, ${50+rand(50)}%, ${rand(25)+25}%)`;
  c3.style.backgroundColor = `hsl(${(y+z+z)%360}deg, ${50+rand(50)}%, ${rand(25)+25}%)`;
}
cc();
<button onclick='cc();'>Create Color Themes!</button>
<div id='c1'>X</div>
<div id='c2'>X</div>
<div id='c3'>X</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda