我怎么循环这个直到我没有箭头

时间:2016-05-18 12:01:58

标签: jquery if-statement

我认为我相当遥远,但我正在尝试创建一个场景,点击id 12a,我想提醒用户输入一个数字并继续输入一个数字,直到他们用完了5个箭头。我哪里出错了?

var arrows = 5;
alert('You have ' + arrows + ' arrows!');
var shot    = prompt('How many arrows would you like to shoot?', '1');
var shotten  = parseInt(shot);

$('#12a').click(function(){
if(isNaN(shotten)){
    alert('You must enter a valid number of arrows!');
} else if(shotten > arrows){
    alert('Sorry, but there are only ' + arrows + ' arrows.  You can not shoot ' + shotten + ' arrows!');
} else if(shotten < 0){
    alert('Sorry, but you cannot shoot less than 0 arrows!');
} else {
    arrows -= shotten;
    alert('Now there are only ' + arrows + ' arrows!');
} 
});



  <p>There's a half-elf running at you. You can shoot him with your arrows if you'd like. </p>
  <p>
    <input type="submit" name="12a" id="12a" value="Let's do it.">
  </p>

1 个答案:

答案 0 :(得分:0)

开发人员在使用JavaScript /网页时应注意一些事项。

  • 如果网页没有按照您打算的方式运行,请先检查浏览器控制台是否存在潜在问题。
  • 通常的错误是因为某些库缺失或无法加载例如jQuery等。
  • JS语法错误
  • 最后是逻辑错误。