I’ve tried the suggestions on the forum. I pushed newWeapon to the inventory array but the console says not only to ‘push’ but also ‘add’ newWeapon to the end of the inventory array. I’m confused because I thought I already added newWeapon to the end of the inventory array with push.
Console:
// running tests
Your buyWeapon function should add the value of newWeapon to the inventory array.
// tests completed
// console output
[ReferenceError: Can’t find variable: currentWeaponIndex]
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
// User Editable Region
function buyWeapon() {
if (gold >= 30) {
gold -= 30;
currentWeapon++;
goldText.innerText = gold;
let newWeapon = weapons[currentWeapon].name;
text.innerText = "You now have a " + newWeapon + ".";
inventory.push(newWeapon);
}
}
// User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6.1 Safari/605.1.15
Challenge Information:
Learn Basic JavaScript by Building a Role Playing Game - Step 91
For some reason the variable currentWeaponIndex has been changed to currentWeapon, in the declaration, and where it’s referenced (twice) in your buyWeapon() function.
That worked! Thank you. I’ve never had to edit outside of the edit region under the instructions before, I guess I wasn’t thinking I’d have to do that.
In Step 8 the variable was declared as currentWeaponIndex, so it must have been changed at some point later. I don’t know the course so not sure if it’s a bug or an inadvertent edit.
the whole project has been recently changed to have that variable named currentWeaponIndex instead of currentWeapon. The old code seems to persist sometimes, I’m sure there is some cache issue somewhere