Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript My code is not working

Go to this link (there is too much code to paste). Gem Hunt 2 (inspect the page for HTML and css). and go to the quests tab and click on Fred the crafter speed through the obviously correct ones then when you get to “ALright I have unlocked it for you go craft a stoen furnace and come back.” and inspect go to the console and click ok, you will see this error.

script.js:1018 Uncaught TypeError: Cannot read properties of undefined (reading 'text') at showTextNode (script.js:1018:42) at selectOption (script.js:1055:9) at HTMLButtonElement.<anonymous> (script.js:1028:52)

JAVASCRIPT

JavaScript:
function quest2() {
    if (stoneAmt >= 25) {
    quest2Popup();
    const textElement = document.getElementById('text1')
    const optionButtonsElement = document.getElementById('option-buttons1')


    let state = {}

    function startGame() {
        state = {}
        showTextNode(1)
    }

    function showTextNode(textNodeIndex) {
    const textNode = textNodes.find(textNode => textNode.id === textNodeIndex)
    textElement.innerText = textNode.text
    while (optionButtonsElement.firstChild) {
        optionButtonsElement.removeChild(optionButtonsElement.firstChild)
    }

    textNode.options.forEach(option => {
        if (showOption(option)) {
        const button = document.createElement('button')
        button.innerText = option.text
        button.classList.add('btn')
        button.addEventListener('click', () => selectOption(option))
        optionButtonsElement.appendChild(button)
        }
    })
    }

    function showOption(option) {
    return option.requiredState == null || option.requiredState(state)
    }

    function selectOption(option) {
    const nextTextNodeId = option.nextText
    if (nextTextNodeId < 0) {
        return startGame()
    }
    if (nextTextNodeId == 50) {
        document.getElementById("craftitem").style.display = "block";
        quest2Popup();
    }

    if (nextTextNodeId == 100) {
        quest2Popup();
    }
    state = Object.assign(state, option.setState)
    showTextNode(nextTextNodeId)
    }


    const textNodes = [
    {
        id: 1,
        text: "Anvils are Black \n Handles are Wood \n And crafting is a Joy!",
        options: [
        {
            text: 'Anvils?',
            nextText: 2
        },
        {
            text: 'I hate poems',
            nextText: 12
        }
        ]
    },
    {
        id: 2,
        text: 'You don\'t know anvils?',
        options: [
        {
            text: 'I don\'t',
            nextText: 3
        }
        ]
    },
    {
        id: 3,
        text: 'They are used for crafting.',
        options: [
        {
            text: 'What is this \'crafting\'',
            nextText: 4
        },
        {
            text: 'Ah I know what this is',
            nextText: 5
        }
        ]
    },
    {
        id: 4,
        text: 'Crafting is the procces of making useful items by using pther items.',
        options: [
        {
            text: 'I get it now',
            nextText: 5
        }
        ]
    },
    {
        id: 5,
        text: 'Good! \n you look smart maybe I can teach you it.',
        options: [
        {
            text: 'Yes Please',
            nextText: 7
        },
        {
            text: 'NO',
            nextText: 6
        }
        ]
    },
    {
        id: 6,
        text: 'Fine.',
        options: [
        {
            text: 'End',
            nextText: 100
        }
        ]
    },
    {
        id: 7,
        text: 'ALright I have unlocked it for you go craft a stoen furnace and come back.',
        options: [
        {
            text: 'Ok',
            nextText: 50
        }
        ]
    },
    {
        id: 8,
        text: 'Thanks! I can find Quartz, emeralds, rubys, and may more!',
        options: [
        {
            text: 'End',
            nextText: 0
        },
        {
            text: 'What is "many more?"?',
            nextText: 9
        }
        ]
    },
    {
        id: 9,
        text: 'As I said before, I can find Quartz, emeralds, rubys.',
        options: [
        {
            text: 'Continue',
            nextText: 10
        }
        ]
    },
    {
        id: 10,
        text: 'The rest is a secret only if I find it I will tell you.',
        options: [
        {
            text: 'Done',
            nextText: 0
        }
        ]
    },
    {
        id: 12,
        text: 'Well that hurt my feelings',
        options: [
        {
            text: 'End',
            nextText: 100
        }
        ]
    }
    ]
    startGame();
    }

    else {
        failQuestspopo();
    }
}
 
Solution
D
Your description "speed through the obviously correct ones" is pretty cryptic but I managed to get to the error. The line and column numbers in the console error stack don't match with what you posted, I guess you've been working on it since creating your post.

The error originates from this entry in your textNodes array:
JavaScript:
{
        id: 7,
        text: 'ALright I have unlocked it for you go craft a stoen furnace and come back.',
        options: [
        {
            text: 'Ok',
            nextText: 50
        }
        ]
    },
so after you have clicked Ok it tries to use textNode #50 which does not exist (there are only 12 of them).
Your description "speed through the obviously correct ones" is pretty cryptic but I managed to get to the error. The line and column numbers in the console error stack don't match with what you posted, I guess you've been working on it since creating your post.

The error originates from this entry in your textNodes array:
JavaScript:
{
        id: 7,
        text: 'ALright I have unlocked it for you go craft a stoen furnace and come back.',
        options: [
        {
            text: 'Ok',
            nextText: 50
        }
        ]
    },
so after you have clicked Ok it tries to use textNode #50 which does not exist (there are only 12 of them).
 
Solution

Buy us a coffee!

Back
Top Bottom