The idea was simple. WHen i push a button it lets oppoSpeed as random number and players speed is always 10. If yourSpeed>=oppoSpeed you attack first. It says that oppoSpeed is undefined but i dont see why. I really want this code to be as simple as this. Can you help?
<body>
<button onclick="spawnEnemy()">Summon Enemy</button> <!--Here the function is triggered-->
<button class="opponent" id="atak" onclick="attackHim()" style="display:block">Atak</button>
<button class="opponent" id="obrona" onclick="defendFromHim()" style="display:block">Obrona</button>
<button class="opponent" id="ucieczka" onclick="runFromHim()" style="display:block">Ucieczka</button>
<script>
let yourHp=10
let yourSpeed=5
let yourDmg=5
let yourDefence=5
console.log("Twoje statystyki na początek wynoszą: " + "\nHp: "+ yourHp+
"\nSpeed: "+yourSpeed+ "\nDmg: "+yourDmg+"\nDefence: "+yourDefence
)
console.log("---------------")
function spawnEnemy(){ //here its defining oppoSpeed
let oppoHp=Math.floor(Math.random() * (11))
let oppoSpeed=Math.floor(Math.random() * (11))
let oppoDmg=Math.floor(Math.random() * (11))
let oppoDefence=Math.floor(Math.random() * (11))
console.log("Statystyki wroga wynoszą: "+ "\nHp: "+oppoHp +
"\nSpeed: "+oppoSpeed+ "\nDmg: "+oppoDmg+"\nDefence: "+oppoDefence)
console.log("---------------")
}
function attackHim(){
if(yourSpeed>=oppoSpeed){ //it shows the problem here
oppoHp=oppoHp-2
console.log("Zadałeś obrażenia! Hp przeciwnika wynosi: "+oppoHp)
if(oppoHp<=0){
console.log("Pokonałeś go! Idź dalej!")
} else if(oppoHp>0){
yourHp=yourHp-2
console.log("Otrzymałeś obrażenia! Twoje hp wynosi: "+yourHp)
}
}else if(yourSpeed<oppoSpeed){
yourHp=yourHp-2
console.log("Otrzymałeś obrażenia! Twoje hp wynosi: "+yourHp)
if(yourHp<=0){
console.log("Przegrałeś! Koniec gry!")
} else if(yourHp>0){
oppoHp=oppoHp-2
console.log("Zadałeś obrażenia! Hp przeciwnika wynosi: "+oppoHp)
}
}
}
</script>
</body>