Learn Basic Debugging by Building a Random Background Color Changer - Step 3

Tell us what’s happening:

My math floor and math random are set to have a max value of 10, yet i keep getting numbers that are way above my set parameters. 70,55,90 and so on. I tried running the code without the darkColorsArr.length and everything worked fine but it was not the answer they were looking for. I do not know what to fix. any help?

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Build a random background color changer</title>
    <link rel="stylesheet" href="./styles.css" />
  </head>
  <body>
    <h1>Random Background Color changer</h1>

    <main>
      <section class="bg-information-container">
        <p>Hex Code: <span id="bg-hex-code">#110815</span></p>
      </section>

      <button class="btn" id="btn">Change Background Color</button>
    </main>
    <script src="./script.js"></script>
  </body>
</html>
/* file: styles.css */
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --yellow: #f1be32;
  --golden-yellow: #feac32;
  --dark-purple: #110815;
  --light-grey: #efefef;
}

body {
  background-color: var(--dark-purple);
  color: var(--light-grey);
  text-align: center;
}

.bg-information-container {
  margin: 15px 0 25px;
  font-size: 1.2rem;
}

.btn {
  cursor: pointer;
  padding: 10px;
  margin: 10px;
  color: var(--dark-purple);
  background-color: var(--golden-yellow);
  background-image: linear-gradient(#fecc4c, #ffac33);
  border-color: var(--golden-yellow);
  border-width: 3px;
}

.btn:hover {
  background-image: linear-gradient(#ffcc4c, #f89808);
}

/* file: script.js */
const darkColorsArr = [
  "#2C3E50",
  "#34495E",
  "#2C2C2C",
  "#616A6B",
  "#4A235A",
  "#2F4F4F",
  "#0E4B5A",
  "#36454F",
  "#2C3E50",
  "#800020",
];

// User Editable Region

function getRandomIndex() {
  console.log(darkColorsArr.length*Math.floor(Math.random()*10));
}
getRandomIndex();

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36

Challenge Information:

Learn Basic Debugging by Building a Random Background Color Changer - Step 3

You have no need for this and a * 10 in your answer.

(The * 10 should be replaced with the length)

1 Like

I see, thank you. I replaced the * 10 with the array length but I’m still getting very high values.

Hello @PinkCrocs !

This link to a previous post on this topic may help you understand it. I used it to understand.

Wishing you good progress on your coding journey. :slightly_smiling_face:

3 Likes

the last block of lines helped a lot here! I got the question correct and I understand the logic behind it. Thank you for the help and the kind words!

1 Like

Happy to hear you have been able to solve the problem.

Keep up the great progress @PinkCrocs ! :slightly_smiling_face:

1 Like

Thank you so much for posting this!! It’s exactly what I needed!

1 Like