I'm currently working on a Rock-paper-scissors project, but ran into a bit of a problem regarding lexical scopping (at least, I believe that's the issue).
The game works by having the user click on one of four options (RPS, and 'clear', which clears the developer console). Once the user clicks any of the first three items, which are part of an array, a sibling number is randomly generated to immediately compare in the getResults() function.
HTML:
<button class = "btn" id = "rock">rock</button>
<button class = "btn" id = "paper">paper</button>
<button class = "btn" id = "scissors">scissors</button>
<button class = "btn" id = "clear">clear</button>
Javascript:
const say = (word) => {console.log(word)};
const rps = {
choiceStack: ["rock", "paper", "scissors"],
rMap: [
["t", "u", "c"],
["c", "t", "u"],
["u", "c", "t"]
],
//Trying to figure out how to carry-on the user's value from getResults()
res: {
get "t" () {say("draw")},
get "u" () {say(`user chose: ${user} you win`)},
get "c" () {say(`computer chose: ${comp} you lose`)},
}
};
const button = document.getElementsByClassName("btn"),
arrBtn = Array.from(button);
let userInput = arrBtn.forEach(list => {
list.addEventListener('click', e => {
list.id === "clear" ? console.clear()
: getResults(arrBtn.indexOf(list), Math.floor(Math.random() * 3))
})
});
//Is there a way to enter the user's input into rps.res methods?
function getResults(user, comp) {
say(rps.res[rps.rMap[user][comp]]);
The issue I'm currently facing is pulling both parameters that were within getResults() into the rps object (specifically the string within rps.res, if that makes sense). If that is possible, is there a proper term that's used for situations like this?