简体   繁体   English

我如何限制功能-(老虎机)仅5圈-使用do / while循环

[英]How can i limit function-(slot play) just for 5 turn - with do/while loop

I wanna create a slot machine with 3 number by javascript - but I want to finish my function when three numbers are equal. 我想用JavaScript用3个数字创建一个老虎机-但是我想在3个数字相等时完成我的功能。 I think that if I write it with do/while it will be work, but I can't do it this is my js code: 我认为如果我用do / while编写它会起作用,但是我做不到,这是我的js代码:

 function myF() {
  var slotOne = Math.floor(Math.random() * 3) + 1;
  var slotTwo = Math.floor(Math.random() * 3) + 1;
  var slotThree = Math.floor(Math.random() * 3) + 1;

  document.getElementById("slotOne").innerHTML = slotOne;
  document.getElementById("slotTwo").innerHTML = slotTwo;
  document.getElementById("slotThree").innerHTML = slotThree;

  if(slotOne == slotTwo &&  slotTwo == slotThree) {
    document.getElementById("slotOne").style.backgroundColor = "#48bd48";
    document.getElementById("slotTwo").style.backgroundColor = "#48bd48";
    document.getElementById("slotThree").style.backgroundColor = "#48bd48";
    document.getElementById("winner").classList.add("show");
  }
 }

this function is set onclick attr of a button tag 此功能在按钮标签的onclick属性中设置

Adding to comment, if you want to run function for no of times then just use a counter variable to check no of attempts: 补充说明一下,如果您想不运行任何功能,则只需使用一个计数器变量来检查尝试次数:

Added a reset button to reset the game. 添加了一个重置​​按钮来重置游戏。

 var counter = 0; function myF() { if (counter != 5) { counter++; document.getElementById("slotLeft").innerHTML = "Try count: " + counter; var slotOne = Math.floor(Math.random() * 3) + 1; var slotTwo = Math.floor(Math.random() * 3) + 1; var slotThree = Math.floor(Math.random() * 3) + 1; document.getElementById("slotOne").innerHTML = slotOne; document.getElementById("slotTwo").innerHTML = slotTwo; document.getElementById("slotThree").innerHTML = slotThree; if (slotOne == slotTwo && slotTwo == slotThree) { document.getElementById("slotOne").style.backgroundColor = "#48bd48"; document.getElementById("slotTwo").style.backgroundColor = "#48bd48"; document.getElementById("slotThree").style.backgroundColor = "#48bd48"; document.getElementById("winner").classList.add("show"); counter = 5; // Edited this line } } else { console.log('Game over'); } } function myF1(){ counter = 0; document.getElementById("slotOne").innerHTML = ""; document.getElementById("slotTwo").innerHTML = ""; document.getElementById("slotThree").innerHTML = ""; } 
 <button onclick="myF()">Check</button> <button onclick="myF1()">Restart Game</button> <div id="slotLeft"> </div> <div id="slotOne"> </div> <div id="slotTwo"> </div> <div id="slotThree"> </div> <div id="winner"> </div> 

  function myF() { var slotOneElem = document.getElementById("slotOne"); var slotTwoElem = document.getElementById("slotTwo"); var slotThreeElem = document.getElementById("slotThree"); var generateRand = function() { return Math.floor(Math.random() * 3) + 1; } return (function () { var slotOne = generateRand(); var slotTwo = generateRand(); var slotThree = generateRand(); slotOneElem.innerHTML = slotOne; slotTwoElem.innerHTML = slotTwo; slotThreeElem.innerHTML = slotThree; if (slotOne === slotTwo && slotTwo === slotThree) { slotOneElem.style.backgroundColor = "#48bd48"; slotTwoElem.style.backgroundColor = "#48bd48"; slotThreeElem.style.backgroundColor = "#48bd48"; document.getElementById("winner").classList.add("show"); // Here is the win return true; } return false; })(); } var finished = myF(); while (!finished) { finished = myF(); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM