Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda