Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
Referencing a function argument from a seperate object?

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?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda