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

184
Views
Subtract an image that is representing a life in a game

Im try to code a mini game. The game has two boxes you can click on. The boxes are assigned a random number between 1 and 2.

When the game starts, you have 5 lives. These lives are represented as 5 pictures of a small human.

When the player gets 3,5 and 7 correct points you get a extra life. Which i have achieved.

Now im trying to subtract a life everytime the player clicks on the wrong box.

function setRandomNumber() {
  randomNumber = Math.floor(Math.random() * 2 + 1);

  if (randomNumber === 1) {

    numberOfRightAnswersSpan.innerHTML = `${++correctNumber}`;

    outputDiv.innerHTML = `Det er riktig svar.`;

    if (correctNumber === 3 || correctNumber === 5 || correctNumber === 7) {
      numberOfLivesDiv.innerHTML += `<img src="images/person1.jpg"/>`

    }
  } else {

    numberOfWrongAnswersSpan.innerHTML = `${++wrongNumber}`;
    outputDiv.innerHTML = `Det er feil svar.`;

  }

}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I made a few changes to your code, but tried to keep it fairly similar to your original. I changed your template literals to strings. I changed your .innerHTML modifications to .innerText modifications. I refactored some whitespace to make your if statement more readable. Instead of adding the img element by appending the html string, I used the .appendChild method and createElement method to make the img elements. I also added a class to the element to make it easier to grab later.

function setRandomNumber() {
    randomNumber = Math.floor(Math.random() * 2 + 1);
    if (randomNumber === 1) {
        numberOfRightAnswersSpan.innerText = ++correctNumber;
        outputDiv.innerText = "Det er riktig svar.";
        if (
          correctNumber === 3 ||
          correctNumber === 5 ||
          correctNumber === 7
        ) {
            const newLife = document.createElement("img");
            newLife.src = "images/person1.jpg";
            newLife.classList.add("life");
            numberOfLivesDiv.appendChild(newLife);
        }
    } else {
        numberOfWrongAnswersSpan.innerText = ++wrongNumber;
        outputDiv.innerText = "Det er feil svar.";

        const aLife = document.querySelector(".life");
        aLife.remove();
    }
}

It is important to note at this point there is not logic to check for how many life exists, or what to do when lives run out. Eventually you may hit an error because there is not element with a class of life.

about 4 years ago · Juan Pablo Isaza Report

0

I would re-render the lives every time the number of lives changes.

let number_of_lives = 5;
const lives_container = document.getElementById("user-lives");

function draw_lives(){
  lives_container.innerHTML = "";
  for(let i = 0; i < number_of_lives; i++){
    const img = document.createElement("img");
    img.setAttribute("alt", "life.png");
    const new_life = img;
    lives_container.appendChild(new_life);
  };
  if(number_of_lives <= 0) lives_container.innerText = number_of_lives;
};

function remove_life() {
  number_of_lives--;
  draw_lives();
};

function add_life() {
  number_of_lives++;
  draw_lives();
};


draw_lives();
#user-lives > * {
  margin: 10px;
  background: #7f7;
}
<div id="user-lives"></div>
<button onclick="add_life();">Add life</button>
<button onclick="remove_life();">Remove life</button>

about 4 years ago · Juan Pablo Isaza 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!