-1

So i have a working function but its still going when i have 0 skill points http://prntscr.com/ns4tfx http://prntscr.com/ns4tld http://prntscr.com/ns4tse as you can see it keeps going up. it shouldn't do anything if skillpoints is at 0

I've tried messing around with if statements to fix it but no luck

Here's my Javascript code:

function HitPointsup(){
    if (SkillPoints>0 && rebirths >=1)  
        SkillPoints = SkillPoints - 1;
        HitPoints = HitPoints + 10;
        document.getElementById('SkillPoints').innerHTML = SkillPoints;
        document.getElementById("HitPoints").innerHTML = HitPoints;

Here's my html code:

 Skill Tree &nbsp; &nbsp; SP:<span id="SkillPoints1">0</span>
                <br />
                <br />
                <button id="HitPointsup" onclick="HitPointsup()">Hp 
up</button> &nbsp; <button id="SkillPoints2" onclick="Mp up()">Mp 
up</button>
                <br />
                <button id="SkillPoints3" onclick="Hp regen()">Hp 
regen</button>
                &nbsp;

I expect the hp up button to only work when i have skill points to spend ( i thought putting if(SkillPoints >0) would be enough). my acual results is you can keep pressing the button and hp keeps going up by 10. skill points stay at 0 tho no negative numbers there.

7
  • This is part of the basic rules in html: 2 elements can not have the same identifier. Here you have 3 times (id = "SkillPoints"); do not be surprised if the behavior of the page is out of control then ! May 23, 2019 at 2:30
  • i mean my code works fine that wasn't a issue. i changed it now. i was going through each of those and making a function. those 2-3 where the ones i had left. i just want my hp to stop increaseing so it makes sense in a upgrade system. it shouldn't increase when u have 0 skill points May 23, 2019 at 2:37
  • onclick="Mp up()" and onclick="Hp regen()" is also bad, if you expect good answers, you must post correct information May 23, 2019 at 2:45
  • i didn't make those functions yet so those buttons dont work on purpose. Im not a professional at html or javascript im just starting up. how was i supose to know its correct to have no space's sorry ;/ the main point of the post was to post the one line showing my HitPointsup format May 23, 2019 at 2:49
  • So you think we do not have to read what you post to understand what you are looking for? May 23, 2019 at 2:52

1 Answer 1

1

You are making a 'single line if', that means that only the first line below the if (SkillPoints>0 && rebirths >=1) is being verified. The other values are getting updated as-well. Try to user brackets like:

function HitPointsup(){
    if (SkillPoints>0 && rebirths >=1){  
        SkillPoints = SkillPoints - 1;
        HitPoints = HitPoints + 10;
    }
    document.getElementById('SkillPoints').innerHTML = SkillPoints;
    document.getElementById("HitPoints").innerHTML = HitPoints;
}
4
  • o wow thanks. i had no idea brackets were that important that makes a lot of sense thanks for clarification. May 23, 2019 at 2:51
  • I'm glad it works. Can you please mark as the correct answer? May 23, 2019 at 2:52
  • sorry new at this May 23, 2019 at 2:58
  • thanks. Ive posted my htm stuff plenty of times. i basically copy pasted first line to make a lot of buttons then orginized them how i wanted them then changed the onclicks / what text was on the button. i really didnt think the id was important if my program works fine and i wasnt useing those buttons. now im working on the hp and mana regen buttons. i need to go back through and check correct answer May 23, 2019 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.