Save the result of a calculation to use afterwards - JS / JavaScript

0

Good night everyone, I have a problem and I have no idea how to solve it, I recently started a web course and the initial subject was JS. So that I could test my knowledge, thanks to the tip of a friend, I decided to create a "game" and managed to develop much of what I had in mind, however after doing the damage calculation after the actual attack of one of the characters and compiling the code again the HP of both of them returns to the default value. My question is how do I save the new HP of the characters to use again until it reaches zero?

Note: I accept other tips also so that I can improve my code / logic

class StatusJob {
constructor(Job, AP, MagicResist, PhisicalResist, Strengh, HP = 1000) {
    this.Job = Job; this.AP = AP; this.MagicResist = MagicResist; this.PhisicalResist = PhisicalResist; this.Strengh = Strengh, this.HP = HP
}
}
Status_Mage = new StatusJob()
Status_Mage.Job = 'Mage'
Status_Mage.AP = 58
Status_Mage.MagicResist = 42
Status_Mage.PhisicalResist = 20
Status_Mage.Strengh = 24
Status_Mage.HP
console.log('Your job is: ${Status_Mage.Job}')
console.log('Your hability power is: ${Status_Mage.AP}')
console.log('Your magic resist is: ${Status_Mage.MagicResist}')
console.log('Your phisical resist is: ${Status_Mage.PhisicalResist}')
console.log('Your strengh is: ${Status_Mage.Strengh}')
console.log('Your hit point is: ${Status_Mage.HP}')
console.log('
')

Status_Warrior = new StatusJob()
Status_Warrior.Job = 'Warrior'
Status_Warrior.AP = 10
Status_Warrior.MagicResist = 25
Status_Warrior.PhisicalResist = 45
Status_Warrior.Strengh = 65
Status_Warrior.HP
console.log('Your job is: ${Status_Warrior.Job}')
console.log('Your hability power is: ${Status_Warrior.AP}')
console.log('Your magic resist is: ${Status_Warrior.MagicResist}')
console.log('Your phisical resist is: ${Status_Warrior.PhisicalResist}')
console.log('Your strengh is: ${Status_Warrior.Strengh}')
console.log('Your hit point is: ${Status_Warrior.HP}')
console.log('
')

function rateCritM(min = 0, max = 100) {
let percentage = Math.random() * (max - min) + min
return this.Math.floor(percentage)
}

function rateCritG(min = 0, max = 100) {
let percentage = Math.random() * (max - min) + min
return this.Math.floor(percentage)
}
function rateAccM(min = 0, max = 100) {
let percentage = Math.random() * (max - min) + min
return this.Math.floor(percentage)
}
function rateAccG(min = 0, max = 100) {
let percentage = Math.random() * (max - min) + min
return this.Math.floor(percentage)
}
function rateEvaM(min = 0, max = 100) {
let percentage = Math.random() * (max - min) + min
return this.Math.floor(percentage)
}
function rateEvaG(min = 0, max = 100) {
let percentage = Math.random() * (max - min) + min
return this.Math.floor(percentage)
}

const percentageCritM = rateCritM()
const percentageCritG = rateCritG()
const rateAccuracyG = rateAccG()
const rateAccuracyM = rateAccM()
const rateEvasionG = rateEvaG()
const rateEvasionM = rateEvaM()

const MageAttack = function () {
if (rateAccuracyM < rateEvasionG) {
    console.log('The Warrior managed to evade his attack! The avoidance rate was: ${rateEvasionG}')
    console.log('Mage's accuracy rate in this attack was: ${rateAccuracyM} %')
    console.log('
     ')
} else if (percentageCritM <= 20 && percentageCritM >= 10) {
    Status_Mage.AP = Status_Mage.AP + (Status_Mage.AP * 1 / 4)
    console.log('Mage's attack had a critical hit on ${percentageCritM} and was 1/4 more powerful for this hit!')
    Status_Warrior.HP = Status_Warrior.HP - (Status_Mage.AP - Status_Warrior.MagicResist)
    console.log('The Warrior got ${Status_Warrior.HP} of hit point, after the mage's assault')
    console.log('Mage's accuracy rate in this attack was: ${rateAccuracyM} %')
    console.log('The Warrior's evasion rate in this attack was: ${rateEvasionG} %')
    console.log('
     ')
} else if (percentageCritM <= 40 && percentageCritM >= 21) {
    Status_Mage.AP = Status_Mage.AP + (Status_Mage.AP * 2 / 4)
    console.log('Mage's attack had a critical hit on ${percentageCritM} and was 2/4 more powerful for this hit!')
    Status_Warrior.HP = Status_Warrior.HP - (Status_Mage.AP - Status_Warrior.MagicResist)
    console.log('The Warrior got ${Status_Warrior.HP} of hit point, after the mage's assault')
    console.log('Mage's accuracy rate in this attack was: ${rateAccuracyM} %')
    console.log('The Warrior's evasion rate in this attack was: ${rateEvasionG} %')
    console.log('
     ')
} else if (percentageCritM <= 60 && percentageCritM >= 41) {
    Status_Mage.AP = Status_Mage.AP + (Status_Mage.AP * 3 / 4)
    console.log('Mage's attack had a critical hit on ${percentageCritM} and was 3/4 more powerful for this hit!')
    Status_Warrior.HP = Status_Warrior.HP - (Status_Mage.AP - Status_Warrior.MagicResist)
    console.log('The Warrior got ${Status_Warrior.HP} of hit point, after the mage's assault')
    console.log('Mage's accuracy rate in this attack was: ${rateAccuracyM} %')
    console.log('The Warrior's evasion rate in this attack was: ${rateEvasionG} %')
    console.log('
     ')
} else if (percentageCritM >= 61) {
    Status_Mage.AP = Status_Mage.AP * 2
    console.log('Mage's attack had a critical hit on ${percentageCritM} and was TWICE more powerful for this hit!!')
    Status_Warrior.HP = Status_Warrior.HP - (Status_Mage.AP - Status_Warrior.MagicResist)
    console.log('The Warrior got ${Status_Warrior.HP} of hit point, after the mage's assault')
    console.log('Mage's accuracy rate in this attack was: ${rateAccuracyM} %')
    console.log('The Warrior's evasion rate in this attack was: ${rateEvasionG} %')
    console.log('
     ')
} else if (percentageCritM <= 10) {
    Status_Warrior.HP = Status_Warrior.HP - (Status_Mage.AP - Status_Warrior.MagicResist)
    console.log('Mage's attack had no critical hit and gave his base damage')
    console.log('The Warrior got ${Status_Warrior.HP} of hit point, after the mage's assault')
    console.log('Mage's accuracy rate in this attack was: ${rateAccuracyM} %')
    console.log('The Warrior's evasion rate in this attack was: ${rateEvasionG} %')
    console.log('
     ')
} 

}

const WarriorAttack = function () {
if (rateAccuracyG < rateEvasionM) {
    console.log('Mage managed to evade his attack! The avoidance rate was: ${rateEvasionM}')
    console.log('Warrior's accuracy rate in this attack was: ${rateAccuracyG} %')
    console.log('
     ')
} else if (percentageCritG <= 20 && percentageCritG >= 10) {
    Status_Warrior.Strengh = Status_Warrior.Strengh + (Status_Warrior.Strengh * 1 / 4)
    console.log('Warrior's attack had a critical hit on ${percentageCritG} and was 1/4 more powerful for this hit!')
    Status_Mage.HP = Status_Mage.HP - (Status_Warrior.Strengh - Status_Mage.PhisicalResist)
    console.log('Mage got ${Status_Mage.HP}, after the warrior's assault')
    console.log('Warrior's accuracy rate in this attack was: ${rateAccuracyG} %')
    console.log('The Mage evasion rate in this attack was: ${rateEvasionM} %')
    console.log('
     ')
} else if (percentageCritG <= 40 && percentageCritG >= 21) {
    Status_Warrior.Strengh = Status_Warrior.Strengh + (Status_Warrior.Strengh * 2 / 4)
    console.log('Warrior's attack had a critical hit on ${percentageCritG} and was 2/4 more powerful for this hit!')
    Status_Mage.HP = Status_Mage.HP - (Status_Warrior.Strengh - Status_Mage.PhisicalResist)
    console.log('Mage got ${Status_Mage.HP} of hit point, after the warrior's assault')
    console.log('Warrior's accuracy rate in this attack was: ${rateAccuracyG} %')
    console.log('The Mage evasion rate in this attack was: ${rateEvasionM} %')
    console.log('
    ')
} else if (percentageCritG <= 60 && percentageCritG >= 41) {
    Status_Warrior.Strengh = Status_Warrior.Strengh + (Status_Warrior.Strengh * 3 / 4)
    console.log('Warrior's attack had a critical hit on ${percentageCritG} e ficou 3/4 mais poderoso para este acerto!')
    Status_Mage.HP = Status_Mage.HP - (Status_Warrior.Strengh - Status_Mage.PhisicalResist)
    console.log('Mage got ${Status_Mage.HP} of hit point, after the warrior's assault')
    console.log('Warrior's accuracy rate in this attack was: ${rateAccuracyG} %')
    console.log('The Mage evasion rate in this attack was: ${rateEvasionM} %')
    console.log('
     ')
} else if (percentageCritG >= 61) {
    Status_Warrior.Strengh = Status_Warrior.Strengh * 2
    console.log('Warrior's attack had a critical hit on ${percentageCritG} and was TWICE more powerful for this hit!!')
    Status_Mage.HP = Status_Mage.HP - (Status_Warrior.Strengh - Status_Mage.PhisicalResist)
    console.log('Mage got ${Status_Mage.HP} of hit point, after the warrior's assault')
    console.log('Warrior's accuracy rate in this attack was: ${rateAccuracyG} %')
    console.log('The Mage evasion rate in this attack was: ${rateEvasionM} %')
    console.log('
     ')
} else if (percentageCritG <= 10) {
    Status_Mage.HP = Status_Mage.HP - (Status_Warrior.Strengh - Status_Mage.PhisicalResist)
    console.log('Warrior's attack had no critical hit and gave his base damage')
    console.log('Mage got ${Status_Mage.HP} of hit point, after the warrior's assault')
    console.log('Warrior's accuracy rate in this attack was: ${rateAccuracyG} %')
    console.log('The Mage evasion rate in this attack was: ${rateEvasionM} %')
} 

}

MageAttack()
WarriorAttack()

console.log('The Warrior got ${Status_Warrior.HP} of current hit point, after the Mage's assault')
console.log('The Mage got ${Status_Mage.HP} of current hit point, after the Warrior's assault')
    
asked by anonymous 07.11.2018 / 02:20

0 answers