【问题标题】:JavaScript Choose your own adventure game random number function in loop problemJavaScript 在循环问题中选择你自己的冒险游戏随机数函数
【发布时间】:2019-07-19 08:45:24
【问题描述】:

我正在编写一个选择你自己的冒险程序,如果选择了一个特定的选项(例如等待),用户会得到一个 1-10 之间的随机数来做俯卧撑(俯卧撑是用户点击提示“确定”按钮,但随机数等于多次)这是我的代码到目前为止,但我不断收到错误。我是一个完全的菜鸟,所以对我放轻松。

 var count = Math.floor((Math.random() * 10) + 1);
var setsOf10 = false;
function pushUps() {
  alert("Nice! Lets see you crank out " + pushUps + "!");
}
if (setsOf10 == pushUp) {
    alert("Nice! Lets see you crank out " + pushUp + "!");
    setsOf10 = true;
  }
for (var i=0; i<count; i++){
  pushUps();
}
  else {
    alert("Really, thats it? Try again");
  }

while ( setsOf10 == false);
}

在玩了一些之后,我可以说我很接近但仍然没有它。再一次,我不是要你为我解决这个问题,只需要关于我做错了什么或遗漏什么的指针。这就是我所拥有的,它给了我我的随机数,我只需要它来允许我点击“确定”按钮,无论随机数分配给我多少次。

    var pushUpSets = Math.floor((Math.random() * 10) + 1);
function pushUps(){
  alert(pushUpSets);
  if (pushUpSets < 3){
    var weak = "Thats it? Weak sauce!";
    alert(weak);
  }
  else{
    alert("Sweet lets get some reps in!");
  }
  for (i=0; i>3; i++){
pushUps(pushUpSets);
}
}

【问题讨论】:

  • alert("Nice! Lets see you crank out " + pushUps + "!"); pushUp。
  • 哦,呵呵!我应该在我的 setsOf10 之后定义 pushUp 吗?像这样... var pushUp = setsOf10; ?
  • pushUp 应该是用户完成的俯卧撑次数正确吗?然后你需要在某个地方改变它,你会希望它在count 下声明,因为你需要从不同的函数中访问它。
  • 您不希望将其设置为与setsOf10 相同,因为这是一个布尔值,您希望在点击确定时添加俯卧撑的数量。
  • 俯卧撑的数量应该是我在 1 - 10 之间的随机数的结果。我应该有 Math.floor((Math.random() * 10) + 1);在我的俯卧撑功能中?而不是我的计数变量?

标签: javascript loops random


【解决方案1】:

在这里,做出选择按钮只是让我们去做俯卧撑的虚拟按钮。每次点击都会减少我们的计数。

// This is important, we use this event to wait and let the HTML (DOM) load
// before we go ahead and code. 
document.addEventListener('DOMContentLoaded', () => {
  document.querySelector('#choice').addEventListener('click', makeChoice);
});

function makeChoice() {
  // Call a method to set random pushups and setup the click event
  setUpPushUp();
  // Here we change the display style of the push up section so that it shows to the player.
  document.querySelector('.activity').style.display = 'block';
}

// The pushups variable is declared at the document level
// This way our setUpPushUp and doPushUp functions have easy access.
let pushUps = 0;

function setUpPushUp() {
  // Create a random number of pushups, in sets of 10.
  // We add an extra 1 so we can call the doPushUp method to initialize.
  pushUps = (Math.floor((Math.random() * 10)+1)*10)+1 ;

  // Add a click event to the push up button and call our doPushUp method on each click.
  document.querySelector('#push').addEventListener('click', doPushUp);
  
  // This is just an init call, it will use the extra 1 we added and place test in our P tag.
  doPushUp();
}


function doPushUp() {
  // Get a reference to our output element, we will put text to player here.
  let result = document.querySelector('p');
  // They have clicked, so remove a push up. 
  pushUps--;
  
  // See if the player has done all the required push ups (i.e. pushUps is 0 or less.)
  if (pushUps > 0) {
    result.innerText = `You need to crank out ${pushUps} pushUps`;
  } else {
    result.innerText = 'Nice work!';
  }

}
.activity {
  display: none;
}
<button id="choice">Make a choice !</button>
<div class="activity">
  <p></p>
  <button id="push">Push</button>
</div>

【讨论】:

  • 你在这里为他解决了所有问题,这显然是一个学校问题......
  • 也许我应该删除?
  • 如果我是你我会的。即使不是为了学校,这个想法似乎是学习如何做事,而且你没有提供完整的教程。
  • 如果你想删除你发布的内容,我只是在寻求帮助我做错了什么,而不是要求你为我解决这个问题。提供的答案不是我将要使用的,因为它不是我问的。忘记我问了,我只是在其他地方寻求有关我的代码的帮助。所以不适合我。
  • 一点也不。我添加了 cmets,所以很清楚我在做什么。 :) 如果有帮助那就太好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
相关资源
最近更新 更多