vendredi 22 mars 2019

This code isn't working & is their a more efficient way to do this rather than using a else if statement?

I'm new to all this, i have a project for college, can't seem to get this to work and it seems to have a lot of repeating code, any help would be great, thanks.

var strength = document.getElementById('getstren');
strength.innerHTML = Math.floor((Math.random() * 17) + 1);

    if (strength == 1) {
        document.getElementById('ath').innerHTML = "-5"

    }else if (strength == 2 || strength == 3) {
        document.getElementById('ath').innerHTML = "-4"

    }else if (strength == 4 || strength == 5) {
        document.getElementById('ath').innerHTML = "-3" 

    }else if (strength == 6 || strength == 7) {
        document.getElementById('ath').innerHTML = "-2"
    }
    else if (strength == 8 || strength == 9) {
        document.getElementById('ath').innerHTML = "-1"
    }
    else if (strength == 10 || strength == 11) {
        document.getElementById('ath').innerHTML = "0"      
    }
    else if (strength == 12 || strength == 13) {
        document.getElementById('ath').innerHTML = "1"
    }
    else if (strength == 14 || strength == 15) {
        document.getElementById('ath').innerHTML = "2"  
    }
    else if (strength == 16 || strength == 17) {
        document.getElementById('ath').innerHTML = "3"
    }
    else if (strength == 18) {
        document.getElementById('ath').innerHTML = "4"
    }

I think it should display a figure for strength and should display another figure for athletics depending on the first figure!

Aucun commentaire:

Enregistrer un commentaire