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

240
Visualizações
how to use a map in evenlistener in javascript

my code works with the map ,m, until i use it on the event listener clicking the button, after that is throws an error and tells me m.get() is not a function, however works prior to the click event, what can i do to fix this and why is this happening??

'use strict';

const poll = {
    question: 'What is your favourite programming language?',
    options: ['0: JavaScript', '1: Python', '2: Rust', '3: C++'],
    // This generates [0, 0, 0, 0]. More in the next section 😃
    answers: new Array(4).fill(0),
    registerNewAnswer(m){
        const answer = Number(prompt(`${this.question}\n${this.options.join('\n')}`));
        answer < 4 && answer >= 0 ? answer : alert('invalid entry try again');
        //console.log(answer);
       m.set(this.options[answer],m.get(poll.options[answer]) + 1)
        
    }
    
}
const fun = function(arr){
    let narr =[];
    for(let i =0;i<arr.length;i++){
        let temp = [];
        temp.push(arr[i]);
        temp.push(0);
        narr.push(temp);
    }
    return narr;
}

const m = new Map(fun(poll.options));

const pollPrompt = poll.registerNewAnswer.bind(poll);
pollPrompt(m);

document.querySelector('.poll').addEventListener('click',poll.registerNewAnswer.bind(poll));
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Issue is when the callback is run on click, m points to the HTMLDOMNode and not map

You can do the following:

const poll = {
    question: 'What is your favourite programming language?',
    options: ['0: JavaScript', '1: Python', '2: Rust', '3: C++'],
    // This generates [0, 0, 0, 0]. More in the next section 😃
    answers: new Array(4).fill(0),
    registerNewAnswer(m){
        console.log('m', m);
        const answer = Number(prompt(`${this.question}\n${this.options.join('\n')}`));
        answer < 4 && answer >= 0 ? answer : alert('invalid entry try again');
        //console.log(answer);
       m.set(this.options[answer],m.get(poll.options[answer]) + 1)
        
    }
    
}
const fun = function(arr){
    let narr =[];
    for(let i =0;i<arr.length;i++){
        let temp = [];
        temp.push(arr[i]);
        temp.push(0);
        narr.push(temp);
    }
    return narr;
}

const m = new Map(fun(poll.options));

const pollPrompt = poll.registerNewAnswer.bind(poll);
pollPrompt(m);

document.querySelector('.poll').addEventListener('click', () => { 
  poll.registerNewAnswer(m) 
});
about 4 years ago · Juan Pablo Isaza Relatório
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