Solution 1 :

In the moveballoons() function the following line:

for (var i = 0; i > gBaloons.length; i++) {

Should be

for (var i = 0; i < gBaloons.length; i++) {

Note: I’ve swapped > with <

I couldn’t the get example running, so there could well be other reasons as to why this is not working but that is definitely an issue.

Problem :

I’ve built a small balloon game , trying to learn the foundations,
anyways I’ve successfully rendered balloons, i can see them both on the page however the move functions doesn’t work,it should do so on page load that I’ve set in the index.html body(the startGame func) , code looks fine,however nothing happens.

var gNextId = 100

var gBaloons = createBalloons()

var gInterval = null

function startGame() {

  renderBaloons();

  gInterval = setInterval(() => {
    moveBalloons();
  }, 500);
}



function renderBaloons() {
  var strHtml = ''
  for (var i = 0; i < gBaloons.length; i++) {
    var balloon = gBaloons[i]
    strHtml += `
    ${balloon.txt}
    `
  }
  document.querySelector('.balloon-container').innerHTML = strHtml
}


function moveballoons() {
  var elballoons = document.querySelectorAll('.balloon')
  for (var i = 0; i > gBaloons.length; i++) {
    var balloon = gBaloons[i]
    var elballoon = elballoons[i]
    balloon.bottom += balloon.speed
    elballoon.style.bottom = balloon.bottom + 'px'
  }
  if (balloon.bottom >= 800) clearInterval(gInterval)
}

function createBalloons() {
  var ballons = [
    createBalloon('A'),
    createBalloon('B'),
    createBalloon('C')
  ];
  return ballons
}

function createBalloon(txt) {
  return {
    id: gNextId++,
    bottom: 0,
    speed: 45,
    txt: txt
  }
}
body {
  background-color: lightblue;
}

.balloon {
  position: absolute;
  width: 170px;
  height: 200px;
  border-radius: 40%;
  bottom: 0;
  transition: 3s;
  background-color: yellow;
  text-align: center;
  font-weight: bold;
}

.balloon1 {
  left: 200px;
  background-color: rgb(3, 61, 23);
}

.balloon2 {
  left: 600px;
  background-color: rgb(182, 24, 50);
}

.fade {
  opacity: 0;
}

Comments

Comment posted by WOUNDEDStevenJones

Could you also include your HTML so we can see a working example?

Comment posted by Captain Jack Sparrow

The HTML is needed to determine where exactly the problem is.

By