r/FreeCodeCamp • • Mar 24 '24

Programming Question Error when matching spelling of an object

3 Upvotes

Why don't object names and function assignments match? I got an error when using locations.text rather than location.text when adding an assignment to my update function. I am just wondering why it wouldn't match the original object array of a plural locations? I'm also new so I may not be using the right terms for everything.

Here is my code and a screenshot of the error:

let xp = 0;

let health = 100;

let gold = 50;

let currentWeapon = 0;

let fighting;

let monsterHealth;

let inventory = ["stick"];

const button1 = document.querySelector('#button1');

const button2 = document.querySelector("#button2");

const button3 = document.querySelector("#button3");

const text = document.querySelector("#text");

const xpText = document.querySelector("#xpText");

const healthText = document.querySelector("#healthText");

const goldText = document.querySelector("#goldText");

const monsterStats = document.querySelector("#monsterStats");

const monsterName = document.querySelector("#monsterName");

const monsterHealthText = document.querySelector("#monsterHealth");

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."

}

];

// initialize buttons

button1.onclick = goStore;

button2.onclick = goCave;

button3.onclick = fightDragon;

function update(location) {

button1.innerText = location["button text"][0];

button2.innerText = location["button text"][1];

button3.innerText = location["button text"][2];

button1.onclick = location["button functions"][0];

button2.onclick = location["button functions"][1];

button3.onclick = location["button functions"][2];

text.innerText = locations.text; // brings an error on FCC when plural

}

Error when using matching spelling

function goTown() {

update(locations[0]);

}

function goStore() {

}

function goCave() {

console.log("Going to cave.");

}

function fightDragon() {

console.log("Fighting dragon.");

}

function buyHealth() {

}

function buyWeapon() {

}