Merlin runs around the house with his favorite ball, stopping at my desk every other minute or so for me to throw the ball for him to catch. This activity is my night after I’m finished working on JavaScript. Today was a quiet day. It began with me helping Tommy fix his truck so he could get to work. I’m not good at fixing cars, so I just watched him.
I’m still waiting for another book to be available from the library. So, right now, I’m reading the second book in the Spanish Love Deception. Bring on the cheese. It’s cute and cheesy and a little cringy to read at times. But it’s alright as I wait for another book. I just found out it was the second book in a series. I accidentally came upon it since it was borrowable from the library. I didn’t even have to put it on hold.
After coding today, I did a few main quests in Final Fantasy XIV. I stopped when my next quest happened to be a dungeon. I have no time for a dungeon right now. Even though EndWalker isn’t everyone’s favorite expansion, I like the story and cutscenes.
We are fending tonight. We have rotisserie chickens to eat. I should have Karissa make some rice in case anyone wants any. The kids will probably want rice. I haven’t had rice in a long while. Being half Asian, rice is a staple in my diet. So it’s weird not having it so often now. I’m cool with it; it just feels funny.
I haven’t put anything down in my planner today. I will get to it tomorrow. Oh, I have a doctor’s appointment tomorrow. This appointment has been rescheduled a few times. The first was them because someone hacked their system. And the second time was me because the weather promised to be awful. All I had to do was tell them where I lived, and they understood. People in the city think we get feet and feet of snow when a storm hits. Well, we have once or twice.
JavaScript notes…
———————————–
Step 118
Now, set the innerText property of monsterName to be the name property of the current monster. Do the same for monsterHealthText and the health property.
function goFight() { update(locations[3]); monsterHealth = monsters[fighting].health; monsterStats.style.display = "block"; monsterName.innerText = monsters[fighting].name; monsterHealthText.innerText = monsters[fighting].health; }
Step 119
Now you can build the attack function. First, update the text message to say The
function attack() { text.innerText = "The " + monsters[fighting].name + " attacks."; }
Step 120
On a new line, add the string You attack it with your
function attack() { text.innerText = "The " + monsters[fighting].name + " attacks."; text.innerText += " You attack it with your " + weapons[currentWeapon].name + "."; }
Step 121
Next, set health to equal health minus the monster’s level. Remember you can get this from the monsters[fighting].level property.
function attack() { text.innerText = "The " + monsters[fighting].name + " attacks."; text.innerText += " You attack it with your " + weapons[currentWeapon].name + "."; health -= monsters[fighting].level; }
Step 122
Set monsterHealth to monsterHealth minus the power of the player’s current weapon. Remember you have the currentWeapon variable and the power property.
function attack() { text.innerText = "The " + monsters[fighting].name + " attacks."; text.innerText += " You attack it with your " + weapons[currentWeapon].name + "."; health -= monsters[fighting].level; monsterHealth -= weapons[currentWeapon].power; }
Step 123
The Math object in JavaScript contains static properties and methods for mathematical constants and functions. One of those is Math.random(), which generates a random number from 0 (inclusive) to 1 (exclusive). Another is Math.floor(), which rounds a given number down to the nearest integer.
Using these, you can generate a random number within a range. For example, this generates a random number between 1 and 5: Math.floor(Math.random() * 5) + 1;.
Following this pattern, use the addition operator (+) to add a random number between 1 and the value of xp to your monsterHealth -= weapons[currentWeapon].power.
monsterHealth -= weapons[currentWeapon].power + Math.floor(Math.random() * xp) + 1;
Step 124
Update healthText.innerText and monsterHealthText.innerText to equal health and monsterHealth.
function attack() { text.innerText = "The " + monsters[fighting].name + " attacks."; text.innerText += " You attack it with your " + weapons[currentWeapon].name + "."; health -= monsters[fighting].level; monsterHealth -= weapons[currentWeapon].power + Math.floor(Math.random() * xp) + 1; healthText.innerText = health; monsterHealthText.innerText = monsterHealth; }
Step 125
Add an if statement to check if health is less than or equal to 0. If it is, call the lose function.
function attack() { text.innerText = "The " + monsters[fighting].name + " attacks."; text.innerText += " You attack it with your " + weapons[currentWeapon].name + "."; health -= monsters[fighting].level; monsterHealth -= weapons[currentWeapon].power + Math.floor(Math.random() * xp) + 1; healthText.innerText = health; monsterHealthText.innerText = monsterHealth; if (health <= 0) { lose(); } }
Step 126
You can make an else statement conditional by using else if. Here's an example:
if (num > 10) {
} else if (num < 5) { } At the end of your if statement, add an else if statement to check if monsterHealth is less than or equal to 0. In your else if, call the defeatMonster function.
function attack() { text.innerText = "The " + monsters[fighting].name + " attacks."; text.innerText += " You attack it with your " + weapons[currentWeapon].name + "."; health -= monsters[fighting].level; monsterHealth -= weapons[currentWeapon].power + Math.floor(Math.random() * xp) + 1; healthText.innerText = health; monsterHealthText.innerText = monsterHealth; if (health <= 0) { lose(); } else if (monsterHealth <= 0) { defeatMonster(); } }
Step 127
At the end of your code, create the defeatMonster and lose functions. Leave them empty for now.
function defeatMonster() { } function lose() { }
Step 128
Inside the dodge function, set text.innerText equal to the string You dodge the attack from the
Category: Uncategorized