Home > Enterprise >  How to get a function to run again on 2nd and 3rd (and so on) click of a html button?
How to get a function to run again on 2nd and 3rd (and so on) click of a html button?

Time:09-18

I'm trying to essentially just make a script that is flipping a coin where 0 = thai food and 1 = viet food after clicking the button. The random number always comes out to 1 (Thai food for some reason). And when I click the button it doesn't keep changing. Would appreciate any help as a beginner thank you!

<h1> What's it going to be? </h1>
    <input type="button" id="random" value="Your food awaits" onclick="foodPicker()" >

<h3 id="food"> </h3>


function foodPicker(){
  let foodNum = Math.floor(Math.random() * 2)
  let result = ''
    if (foodNum = 1) {
        result = 'Thai';
    }
        else {
             result = 'Viet';
        }
  document.getElementById('food').innerHTML = result;
}


CodePudding user response:

if (foodNum = 1) { should be if (foodNum == 1) {

  • Related