0

It should randomize the location of a black circle when pressed but I can’t figure out why it isn’t working.

function myFunction1(button) {
  var xPosition = math.floor(math.random() * 101) + '%';
  button.style.top = xPosition;
  var yPosition = math.floor(math.random() * 101) + '%';
  button.style.left = yPosition;
}
.circle {
  width: 100px;
  height: 100px;
  background-color: red;
  border-radius: 50%;
}

.black {
  background-color: black;
}
<button type="button" class="black circle" onclick="myFunction1(this)"></button>

3
  • 2
    Math not math. You also need to set the position property via CSS to something like relative or absolute in order for the top and left to matter Commented Oct 26, 2021 at 15:31
  • See above. But even with that fix you still won't see it work, for same reason that this doesn't work even if you don't use JS. Set a top and left value on your CSS directly, to simulate "a single random location" and look at the result: what happens? Commented Oct 26, 2021 at 15:31
  • It’s working now thank you :) Commented Oct 26, 2021 at 15:46

1 Answer 1

1

Try this:

Change math... to Math..

And add this to css:

.circle {
  ...
  position: fixed;
}

Like this:

function myFunction1(button) {
  var xPosition = Math.floor(Math.random() * 101) + '%';
  button.style.top = xPosition;
  var yPosition = Math.floor(Math.random() * 101) + '%';
  button.style.left = yPosition;
}
.circle {
  width: 100px;
  height: 100px;
  background-color: red;
  border-radius: 50%;
  position: fixed;
}

.black {
  background-color: black;
}
<button type="button" class="black circle" onclick="myFunction1(this)"></button>

Sign up to request clarification or add additional context in comments.

1 Comment

If this helped you mark it correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.