While most of the country has been getting snow and feeling colder than usual, it’s been “warm” here. Warm being in the 40s. It’s still cold here, and the pellet stove’s fire makes the weather cozy. I want to make some hot tea when I’m done writing. I wouldn’t mind some snow.
So, I read two books last week and this past weekend. The first is Spanish Love Deception. It wasn’t too bad. It claims to be an enemy to lovers when they don’t hate each other. The author tried hard to make us believe they were enemies, but it wasn’t there. Though it was a better read than the next book I read.
The second book was It Ends With Us. What can I say? This book is marketed as a romance, but nothing is romantic about it. I worry cause this book is so popular, and many readers are seeing this as romantic and not picking up all the red flags. This book glorifies unhealthy behavior. I think if the author marketed this book as a look into domestic abuse and how it affects the victims, then I could be behind this. But this is marketed as a romance, telling readers, “There is no such thing as bad people, just people doing bad things.” It seems the author is making excuses for the abuser’s behavior. This book has a lot of upsetting material, including suicide, rape, and homelessness, and should have been marketed as such. Maybe put trigger warnings in the beginning. I wonder how many read this book thinking it is a light romance, then have the rug pulled out from under them when they realize it indeed isn’t a romance novel, to say the least. I didn’t particularly appreciate how the story ended. I feel the author didn’t give the character any depth, which was two-dimensional. There was just no foundation to it.
Saturday was pretty relaxing. Tommy and I took Merlin for a walk for exercise. Sunday, we watched football. Sadly, the Bucs aren’t continuing in the playoffs. There is next year.
So, a new expansion is coming out on Final Fantasy XIV next summer. I must finish all the main quests to start a new expansion. I have to get on that.
I’m understanding this coding better. Some of it can be frustrating, but I’m getting it.
I should get back to work on coding.
JavaScript notes…
—————————————-
Step 96
Once a player has the best weapon, they cannot buy another one. Wrap all of the code in your buyWeapon function inside another if statement. The condition should check if currentWeapon is less than 3 – the index of the last weapon.
function buyWeapon() { if (currentWeapon < 3) { if (gold >= 30) { gold -= 30; currentWeapon++; goldText.innerText = gold; let newWeapon = weapons[currentWeapon].name; text.innerText = "You now have a " + newWeapon + "."; inventory.push(newWeapon); text.innerText += " In your inventory you have: " + inventory; } else { text.innerText = "You do not have enough gold to buy a weapon."; } } }
Step 97
Arrays have a length property that returns the number of items in the array. You may want to add new values to the weapons array in the future.
Change your if condition to check if currentWeapon is less than the length of the weapons array. An example of checking the length of an array myArray would look like myArray.length.
function buyWeapon() { if (currentWeapon < weapons.length) { if (gold >= 30) { gold -= 30; currentWeapon++; goldText.innerText = gold; let newWeapon = weapons[currentWeapon].name; text.innerText = "You now have a " + newWeapon + "."; inventory.push(newWeapon); text.innerText += " In your inventory you have: " + inventory; } else { text.innerText = "You do not have enough gold to buy a weapon."; } } }
Step 98
You now have an error to fix. The currentWeapon variable is the index of the weapons array, but array indexing starts at zero. The index of the last element in an array is one less than the length of the array.
Change the if condition to check weapons.length – 1, instead of weapons.length.
function buyWeapon() { if (currentWeapon < weapons.length - 1) { if (gold >= 30) { gold -= 30; currentWeapon++; goldText.innerText = gold; let newWeapon = weapons[currentWeapon].name; text.innerText = "You now have a " + newWeapon + "."; inventory.push(newWeapon); text.innerText += " In your inventory you have: " + inventory; } else { text.innerText = "You do not have enough gold to buy a weapon."; } } }
Step 99
Add an else statement for your outer if statement. Inside this new else statement, set text.innerText to You already have the most powerful weapon!.
function buyWeapon() { if (currentWeapon < weapons.length - 1) { if (gold >= 30) { gold -= 30; currentWeapon++; goldText.innerText = gold; let newWeapon = weapons[currentWeapon].name; text.innerText = "You now have a " + newWeapon + "."; inventory.push(newWeapon); text.innerText += " In your inventory you have: " + inventory; } else { text.innerText = "You do not have enough gold to buy a weapon."; } } else { text.innerText = "You already have the most powerful weapon!" } }
Step 100
Once a player has the most powerful weapon, you can give them the ability to sell their old weapons.
In the outer else statement, set button2.innerText to Sell weapon for 15 gold. Also set button2.onclick to the function name sellWeapon.
function buyWeapon() { if (currentWeapon < weapons.length - 1) { if (gold >= 30) { gold -= 30; currentWeapon++; goldText.innerText = gold; let newWeapon = weapons[currentWeapon].name; text.innerText = "You now have a " + newWeapon + "."; inventory.push(newWeapon); text.innerText += " In your inventory you have: " + inventory; } else { text.innerText = "You do not have enough gold to buy a weapon."; } } else { text.innerText = "You already have the most powerful weapon!"; button2.innerText = "Sell weapon for 15 gold"; button2.onclick = sellWeapon } }
Step 101
Create an empty sellWeapon function.
function sellWeapon() { }
Step 102
Players should not be able to sell their only weapon. Inside the sellWeapon function, add an if statement with a condition that checks if the length of the inventory array is greater than 1.
function sellWeapon() { if (inventory.length > 1) { } }
Step 103
Inside the if statement, set gold equal to 15 more than its current value. Also update goldText.innerText to the new value.
function sellWeapon() { if (inventory.length > 1) { gold += 15; goldText.innerText = gold; } }
Step 104
Use the let keyword to create a variable named currentWeapon. Don’t assign it a value yet.
Notice that you already have a currentWeapon variable elsewhere in your code. Since you are using the let keyword instead of var, the new currentWeapon is scoped only to this if statement. At the close of the if statement, the old currentWeapon will be used again.
function sellWeapon() { if (inventory.length > 1) { gold += 15; goldText.innerText = gold; let currentWeapon; } }
Step 105
The shift() method on an array removes the first element in the array and returns it. Use this method to take the first element from the inventory array and assign it to your currentWeapon variable.
function sellWeapon() { if (inventory.length > 1) { gold += 15; goldText.innerText = gold; let currentWeapon = inventory.shift(); } }
Step 106
After your currentWeapon, use the concatenation operator to set text.innerText to the string You sold a , then currentWeapon, then the string ..
function sellWeapon() { if (inventory.length > 1) { gold += 15; goldText.innerText = gold; let currentWeapon = inventory.shift(); text.innerText = "You sold a " + currentWeapon + "." } }
Step 107
Now use the += operator to add the string In your inventory you have: and the contents of inventory to the text.innerText. Make sure to include the space at the beginning and end of the In your inventory you have: string.
function sellWeapon() { if (inventory.length > 1) { gold += 15; goldText.innerText = gold; let currentWeapon = inventory.shift(); text.innerText = "You sold a " + currentWeapon + "."; text.innerText += " In your inventory you have: " + inventory; } }
Step 108
Use an else statement to run when the inventory length is not more than one. Set the text.innerText to say Don’t sell your only weapon!.
function sellWeapon() { if (inventory.length > 1) { gold += 15; goldText.innerText = gold; let currentWeapon = inventory.shift(); text.innerText = "You sold a " + currentWeapon + "."; text.innerText += " In your inventory you have: " + inventory; } else { inventory.length > 1; text.innerText = "Don't sell your only weapon!"; } }
Step 109
Now you can start the code to fight monsters. To keep your code organized, your fightDragon function has been moved for you to be near the other fight functions.
Below your weapons array, define a monsters variable and assign it an array. Set that array to have three objects, each with a name, level, and health properties. The first object’s values should be slime, 2, and 15, in order. The second should be fanged beast, 8, and 60. The third should be dragon, 20, and 300.
const monsters = [ { name: "slime", level: 2, health: 15 }, { name: "fanged beast", level: 8, health: 60 }, { name: "dragon", level: 20, health: 300 } ];
Step 110
Fighting each type of monster will use similar logic. Create an empty function called goFight to manage this logic.
function goFight() { }
Step 111
In your fightSlime function, set fighting equal to 0 – the index of slime in the monsters array. Remember that you already declared fighting earlier in your code, so you do not need let or const here.
On the next line, call the goFight function.
function fightSlime() {
fighting = 0;
goFight();
}
Step 112
Following the same pattern, use that code in the fightBeast and fightDragon functions. Remember that beast is at index 1 and dragon is at index 2. Also, remove the console.log call from your fightDragon function.
function fightBeast() { fighting = 1; goFight(); } function fightDragon() { fighting = 2; goFight(); }
Step 113
At the end of your code, create two empty functions named attack and dodge.
function attack() { } function dodge() { }
Step 114
Add a new object to the end of the locations array, following the same properties as the rest of the objects. Set name to fight, button text to an array with Attack, Dodge, and Run, button functions to an array with attack, dodge, and goTown, and text to You are fighting a monster..
const locations = [ { name: "town square", "button text": ["Go to store", "Go to cave", "Fight dragon"], "button functions": [goStore, goCave, fightDragon], text: "You are in the town square. You see a sign that says \"Store\"." }, { name: "store", "button text": ["Buy 10 health (10 gold)", "Buy weapon (30 gold)", "Go to town square"], "button functions": [buyHealth, buyWeapon, goTown], text: "You enter the store." }, { name: "cave", "button text": ["Fight slime", "Fight fanged beast", "Go to town square"], "button functions": [fightSlime, fightBeast, goTown], text: "You enter the cave. You see some monsters." }, { name: "fight", "button text": ["Attack", "Dodge", "Run"], "button functions": [attack, dodge, goTown], text: "You are fighting a monster." } ];
Step 115
In the goFight function, call your update function with the fourth object in locations as an argument.
function goFight() { update(locations[3]); }
Step 116
Below your update call, set the monsterHealth to be the health of the current monster. You can get this value by accessing the health property of monsters[fighting] with dot notation.
function goFight() { update(locations[3]); monsterHealth = monsters[fighting].health; }
Step 117
The HTML element that shows the monster’s stats has been hidden with CSS. Display the monsterStats element by updating the display property of the style property to block. For example, updating the first property of the name property of user would look like:
user.name.first = “Naomi”;
function goFight() { update(locations[3]); monsterHealth = monsters[fighting].health; monsterStats.style.display = "block"; }