CuddlyDaTurtle
CuddlyDaTurtle

Reputation: 17

Javascript lives system broken in web design

so I'm coding a quiz on HTML as a competition with my friend, and I'm making a lives system but it seems to do skip the fact that there are lives 2 and 3 and do what it would do if you lost a life when you had 1 life.

JavaScript:

var lives = 3;
var loadLives = setInterval(showLives, 100);

function loseLife() {
    if (lives = 1) {
        location.href='fail.html';
        lives = 3;
    }
    else {
        lives = lives - 1;  
    }
}

function showLives() {
    document.getElementById("lifeamount").innerHTML = lives;
}

HTML:

<a class="wa" href="javascript:loseLife()">Here?</a>

I do have more HTML but that's the line that executes the function

Upvotes: 0

Views: 45

Answers (2)

CuddlyDaTurtle
CuddlyDaTurtle

Reputation: 17

dont worry i somehow found a solution :D

var lives = 3;
var loadLives = setInterval(showLives, 100);

function loseLife() {
    if (lives > 1) {
        lives = --lives;
    }
    else {
        location.href='fail.html';
    }
}

function showLives() {
    document.getElementById("lifeamount").innerHTML = lives;
}


and same html

Upvotes: 1

aletzo
aletzo

Reputation: 2481

This line is the problem:

if (lives = 1) {

Right now, it's not comparing lives to 1, but it's assigning 1 to lives. You'd need something like this instead:

if (lives === 1) {

Upvotes: 1

Related Questions