jeudi 8 juillet 2021

How do I prevent my Javascript Code from selecting multiple random numbers instead of 1

When I select the random_guess.eventListener() I the number logs multiple random numbers from function - I only need 1. How would I be able to achieve that outcome?

let randomized_answer = document.getElementById("computer_answer");
let random_guess = document.querySelector(".guess");

let array = []

// Initalizing User Input on {-} & {+} to react with number selection = {counter}
let counter = document.getElementById("counter");
let add = document.querySelector(".add");
let minus = document.querySelector(".minus");

// Initalizing Score Counter
let score = document.getElementById("score")
let score_count = 0

//Function that Incrementse or Decrements Score Depending on {-} {+} selection.

var count = 0;
add.addEventListener("click", () => {
    if (count >9){
        return
    }else {
    count ++;
    console.log(count);
    counter.innerHTML = count;

    minus.addEventListener("click" , () =>{
        if (count < 1){
            return
        }else {
        count --;
        counter.innerHTML = count;
    }});

    random_guess.addEventListener("click", () => {
            let comptuer_select = Math.floor(Math.random()*11);
            randomized_answer.innerHTML = comptuer_select;
            console.log(comptuer_select,"Computer select");
    
        if (count === comptuer_select) {
            score_count ++
            score.innerHTML = score_count;
        }else if (count !== comptuer_select) {
         return;
        }
    });
  
}}

CONSOLE LOG BELOW What is happening its accessing multiple random values on one click, instead of accessing one random value? What is happening?

 4 "Computer select"
NumberGuesser.js:54 3 "Computer select"
NumberGuesser.js:54 0 "Computer select"
NumberGuesser.js:54 5 "Computer select"
NumberGuesser.js:54 3 "Computer select"



Aucun commentaire:

Enregistrer un commentaire