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

Tell us what’s happening:

My first guess was to use Math.floor; I don’t think I am doing it right. Please 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.random(), Math.floor(), Math.round());
}
getRandomIndex();

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

Challenge Information:

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

Yes, it’s correct to use Math.floor(). But your syntax is wrong.

You can take a look at the documentation on MDN for the correct syntax of Math.floor():

I’m stumped. Please give me another clue.

Example:

console.log(3.12345);

will print out: 3.12345.

console.log(Math.floor(3.12345));

will print out: 3

function getRandomIndex() {

console.log(darkColorsArr.length * Math.random(), Math.floor(0.015882899879771095,

2.114596286197641,

6.040964780197666));

}

getRandomIndex();

:/?

@Alex0

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

This function will print out something like: 2.114596286197641
because the expression
darkColorsArr.length * Math.random() is return a float number: 2.114596286197641.

Now, if we want this function to print out a rounded down integer number: 2,

we have to round down the expression
darkColorsArr.length * Math.random()
by using Math.floor().

To do that, we put the whole expression
darkColorsArr.length * Math.random()
inside Math.floor() as an argument.

2 Likes

SWEET JESUS YOU’RE RIGHT DEWD

1 Like

im on step 2 and it keeps saying math.radom has a capitalization error

const darkColorsArr = [
  "#2C3E50",
  "#34495E",
  "#2C2C2C",
  "#616A6B",
  "#4A235A",
  "#2F4F4F",
  "#0E4B5A",
  "#36454F",
  "#2C3E50",
  "#800020",
];
function getRandomIndex() {
 console.log(Math.floor(darkColorsArr.length * Math.random())
}
getRandomIndex();

my code so far, now i dont know what im doing wrong … can anyone help

hey @awaisraf201

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.