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

234
Views
HTML element "top" style being set generated random number but not moving to the position in JavaScript

I have been working on a project and remaking snake. The code is all supposed to set the position of the apple to a random number and it logs it as doing so, but the apple stays in the same place. The position type is "relative" so it stays within a box. This is my first time using HTML and Javascript together. The code just doesn't work (no errors or warnings). The random number generation code is as follows:

function rand(min, max, step) {
    var delta,
        range,
        rand;
    if (arguments.length < 2) {
        max = min;
        min = 0;
    }
    if (!step) {
        step = 1;
    }
    delta = max - min;
    range = delta / step;
    rand = Math.random();
    rand *= range;
    rand = Math.floor(rand);
    rand *= step;
    rand += min;
    return parseInt(rand);
}

The code for setting the "apples" position (in js):

function generateApple() {
    let y = parseInt(rand(-15, 470, 15))
    apple.style.top = y
    console.log(apple.style.top)
}

It does successfully print the new location but doesn't move the "apple" HTML object to the position. I have the initial position set to 15 in style.left and 15 in style.top. The CSS for the apple is as follows:

.apple {
    height: 15px;
    width: 15px;
    background-color: red;
    border: 2px;
}

The code that sets the position as relative when the window loads is as follows

window.addEventListener('load', () => {
    snake.style.position = 'relative';
    apple.style.position = 'relative'
    snake.style.left = 105;
    snake.style.top = 105;
    apple.style.top = 15;
    apple.style.left = 15;
    if (highScore == null) {
        window.localStorage.setItem('high_score', 0)
    }
});

The full code is on this website: https://passionproject.on.fleek.co

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Window load event is fired after the JavaScript that is positioning the apple has been run. If you put could breakpoints in your code, you will see that first it moves the apple into the right position and then the load event listener fires.

Since your JS is at the bottom of the page, the simplest fix really is to not use load event listener and just do it immediately.

about 4 years ago · Santiago Trujillo 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!