JavaScript RPG tutorial from @BeauCarnes/JavaScript-RPG#script.js

sorry about the poor formatting of my question, i am new to Replit.

this tutorial generates this error when run: ReferenceError: restart is not defined

however it seem to be defined in this array:

const locations = [
    name: "lose",
    "button functions":[restart, restart, restart],
    "button text": ["REPLAY ?", "REPLAY ?", "REPLAY ?"],
    text: "You die. "

and the function is

  function restart() {
    xp = 0;
    health = 100;
    gold = 50;
    currentWeapon = 0;
    inventory = ["stick"];
    goldText.innerText = gold;
    healthText.innerText = health;
    xpText.innerText = xp;

also in function restart()
the function name - restart has squiggle lines underneath and when hovered over states:

restart is declared but its value is never read.

i have tried to be sure that this code, is type in the same as the tutorial but i don’t see any differences that would cause the runtime error i’m getting.

suggestions would be appreciated, thanks

Hi @anuglybug please could you send a link to your repl, thanks!

The restart function is defined within function fightDragon, so it can’t be accessed from the main code. Move the restart function to anywhere that isn’t within {}.


i had been stuck here over days.
i moved it like you said and it now works, thanks to you.

i may not have the concept yet but think the error is along the line of globe vs local:
i will now spend time thinking about one function being coded inside of another function as you explained, caused the error.

as i said i’m new to all of this and unfamiliar to posting.
here is what i have been using.

many thanks to Umar Sharief for the solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.