repo: https://github.com/christinabohn/botany-game
Code: Select all
const button = document.querySelector('#button')
const objects = [
{
id: 0,
name: "Object",
"first question": {
"button text": "button text",
"button function": "outerFunction.innerFunction2"
},
"second question": {
"button text": "button text",
"button function": "otherFunction"
}
}
]
const actions = [
{
name: "welcome",
"button text": "Begin!",
"button function": "outerFunction.innerFunction1"
}
]
function useRandomIndex() {
let randomIndex = Math.floor(Math.random() * objects.length);
let currentObject = objects[randomIndex];
function innerFunction1() {
button.innerText = currentObject["first question"]["button text"][0]
button.onclick = currentObject["first question"]["button function"][0]
}
function innerFunction2() {
button.innerText = currentObject["second question"]["button text"][0]
button.onclick = currentObject["second question"]["button function"][0]
}
button.onclick = innerFunction1
return { innerFunction1, innerFunction2 }
};
const outerFunction = useRandomIndex();
function update(action) {
button.innerText = action["button text"][0]
button.innerText = action["button functions"][0]
}
function welcome() {
update(actions[0])
}
functions['outerFunction.innerFunction1'] = outerFunction.innerFunction1;
functions['outerFunction.innerFunction2'] = outerFunction.innerFunction2;
functions['update'] = update;
functions['welcome'] = welcome;
welcome();