let state = {}
var Playername;
At the start of the code the var Playername is created.
if (nextTextNodeId == 60) {
var InputName = document.getElementById('name').value;
let Playername = InputName;
alert(Playername);
if (Playername == "Andrew") {
ToggleInputOff()
state = Object.assign(state, option.setState)
showTextNode(4)
return;
}
else if (Playername == "") {
state = Object.assign(state, option.setState)
showTextNode(3)
return;
}
else {
ToggleInputOff()
state = Object.assign(state, option.setState)
showTextNode(5)
return;
}
return ToggleInputOff()
}
The alert Playername displays the inputted name in the alert box correctly, however when i go to use this variable later in the project it comes up as undefined.
{
id: 5,
text: "Welcome " + Playername + " let us start the day!",
options: [
{
text: "Let's go",
nextText: 6
}
]
},
The text nodes are accessed through this function and displayed on the screen
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)
}
})
}
Whenever this text node is accessed the output is
Welcome Undefined let us start the day!