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

163
Visualizações
How to move two cars across the screen using JavaScript

Currently I have two pages the CarRace.html the second is JavaScript.js. CarRace contains three buttons and two images (each is a picture of a car). When the StartRace button is clicked, a random number is to be generated for each car and is to repeat every second. The two numbers generated is the distance each image should move. Currently nothing moves.

CarRace Code:

var timer;
var ArandomNumber;
var BrandonNumber;
var x = 0
var q = 0

function GatherData() {
  ArandomNumber = GetRandomNumA();
  BrandonNumber = GetRandomNumB();

  var thedivtop = document.getElementById("Move1");
  x += 10;
  thedivtop.style.left = x + 'px';
  var thedivbottom = document.getElementById("Move2");
  q += 10;
  thedivbottom.style.left = q + 'px';
}

function StartRace() {
  timer = setInterval(GatherData, 1000);

}

function GetRandomNumA() {
  var x = Math.random();
  x = Math.random() + 55;
  return x;
}

function GetRandomNumB() {
  var q = Math.random();
  q = Math.random() + 55;
  return q;
}
<title>Race</title>
<script src="JavaScript.js"></script>
<style>
  .moveable {
    position: absolute;
    height: 100px;
    width: 200px;
  }
</style>
</head>

<body>
  <b>First to 800 pixels wins!</b>
  <br>
  <br>
  <b>Both cars start at 0 pixels.</b>
  <br>
  <br>
  <input id="Button1" type="button" value="Start Race" onclick="StartRace()" />
  <input id="Button1" type="button" value="Pause Race" onclick="PauseRace()" />
  <input id="Button1" type="button" value="Reset Cars" onclick="ResetRace()" />
  <br>
  <br>
  <div id="Move1">
    <img id="Car1" class="moveable" src="delorean.jpeg" />
  </div>
  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <div id="Move2">
    <img id="Car2" class="moveable" src="duster.jpg" />
  </div>


</body>

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

0

Your JavaScript code does change the cars' left property, however you need to modify your CSS in order to allow the left property take effect.

After adding relative position to the cars elements, they move as expected.

var timer;
var ArandomNumber;
var BrandonNumber;
var x = 0
var q = 0

function GatherData() {
  ArandomNumber = GetRandomNumA();
  BrandonNumber = GetRandomNumB();

  var thedivtop = document.getElementById("Move1");
  x += 10;
  thedivtop.style.left = x + 'px';
  var thedivbottom = document.getElementById("Move2");
  q += 10;
  thedivbottom.style.left = q + 'px';
}

function StartRace() {
  timer = setInterval(GatherData, 1000);
}

function GetRandomNumA() {
  var x = Math.random();
  x = Math.random() + 55;
  return x;
}

function GetRandomNumB() {
  var q = Math.random();
  q = Math.random() + 55;
  return q;
}
#Move1, #Move2 {
  position: relative;
}
<title>Race</title>
<script src="JavaScript.js"></script>
<style>
  .moveable {
    position: absolute;
    height: 100px;
    width: 200px;
  }
</style>
</head>

<body>
  <b>First to 800 pixels wins!</b>
  <br>
  <br>
  <b>Both cars start at 0 pixels.</b>
  <br>
  <br>
  <input id="Button1" type="button" value="Start Race" onclick="StartRace()" />
  <input id="Button1" type="button" value="Pause Race" onclick="PauseRace()" />
  <input id="Button1" type="button" value="Reset Cars" onclick="ResetRace()" />
  <br>
  <br>
  <div id="Move1">
    <img id="Car1" class="moveable" src="delorean.jpeg" />
  </div>
  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <div id="Move2">
    <img id="Car2" class="moveable" src="duster.jpg" />
  </div>


</body>

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