首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置条件比较两个值并执行指定函数

如何设置条件比较两个值并执行指定函数
EN

Stack Overflow用户
提问于 2018-02-20 05:58:56
回答 2查看 53关注 0票数 0

这段代码中的指令应该是:

将targetNumber与totalScore进行比较

代码语言:javascript
复制
$(document).ready(function() {

  var totalScore = 0;
  

  var targetNumber = Math.floor(Math.random() * 120) + 12;
  $("#targetNumber").html(targetNumber);

  var ruby = Math.floor(Math.random() * 10) + 1;
  console.log(ruby);
  var diamond = Math.floor(Math.random() * 10) + 1;
  console.log(diamond);
  var emerald = Math.floor(Math.random() * 10) + 1;
  console.log(emerald);
  var bloodstone = Math.floor(Math.random() * 10) + 1;
  console.log(bloodstone);

  $("#ruby").click(function() {
    totalScore = totalScore + ruby;
    console.log("totalScore");
    $("#totalScore").html(totalScore + ruby);
  });

  $("#diamond").click(function() {
    totalScore = totalScore + diamond;
    console.log("totalScore");
    $("#totalScore").html(totalScore + diamond);
  });

  $("#emerald").click(function() {
    totalScore = totalScore + emerald;
    console.log("totalScore");
    $("#totalScore").html(totalScore + emerald);
  });

  $("#bloodstone").click(function() {
    totalScore = totalScore + bloodstone;
    console.log("totalScore");
    $("#totalScore").html(totalScore + bloodstone);
  });
});

$(document).ready(function() {

    var totalScore = 0;
    console.log(totalScore);
    $("#totalScore").html(totalScore);
  
    var ruby = Math.floor(Math.random() * 10) + 1;
    console.log(ruby);
    var diamond = Math.floor(Math.random() * 10) + 1;
    console.log(diamond);
    var emerald = Math.floor(Math.random() * 10) + 1;
    console.log(emerald);
    var bloodstone = Math.floor(Math.random() * 10) + 1;
    console.log(bloodstone);
  
    var wins = 0;
    $("#wins").html(wins)
    var losses = 0;
    $("#losses").html(losses)
    
    $("#ruby").click(function() {
      totalScore = totalScore + ruby;
      console.log("totalScore");
      $("#totalScore").html(totalScore + ruby);
    });
  
    $("#diamond").click(function() {
      totalScore = totalScore + diamond;
      console.log("totalScore");
      $("#totalScore").html(totalScore + diamond);
    });
  
    $("#emerald").click(function() {
      totalScore = totalScore + emerald;
      console.log("totalScore");
      $("#totalScore").html(totalScore + emerald);
    });
  
    $("#bloodstone").click(function() {
      totalScore = totalScore + bloodstone;
      console.log("totalScore");
      $("#totalScore").html(totalScore + bloodstone);
    });

    if (totalScore < targetNumber); {
		$("#totalScore").html(totalScore);
		return(totalScore);
	}
	if (totalScore > targetNumber); {
		$("#totalScore").html(totalScore);
		alert("you have exceeded the target number - Try again.");
		losses++;
		$("#losses").html(losses);
		start();
    }
	if (totalScore = targetNumber); {
		$("#totalScore").html(totalScore);
		wins++;
		$("#wins").html(wins);
        alert("Nailed it!");
    }})
代码语言:javascript
复制
body {
    background: #D2B48C;
  }
  
  ul {
    margin: 0 auto;
    margin-top:50px;
    padding: 0;
    width: 320px;
  }
  
  ul li {
    list-style: none;
    display: inline-block;
  }
代码语言:javascript
复制
<!--
    javascript

    Crystals: Ruby, Diamond, Emerald, Blodstone
        value between 1-10
        each click adds crystal value to totalScore

    targetNumber
        randomly generated

    totalScore
        sum of crystal clicks
    
    WinsLosses
        If totalScore = targetNumber + 1 Win and Reset
        If totalScore > targetNumber + 1 Loss and Reset
    -->

    
     <!DOCTYPE html>
     <html lang="en">
     <head>
    <link rel="stylesheet" type="text/css" href="assets/css/style.css">
         <meta charset="UTF-8">
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
         <meta http-equiv="X-UA-Compatible" content="ie=edge">
         <title>Crystal Game</title>
     </head>
     <body>
            <textarea id = "targetNumber" rows="4" cols="50"> 
                    "Hit this number without going over" 
                    </textarea>  (targetNumber)

            <textarea id = "totalScore" rows="4" cols="50"> 
                    "Your total score equals" 
                    </textarea>  (totalScore)
                    
            <textarea id = "wins" rows="4" cols="50"> 
                    "wins" 
                    </textarea>  (wins)

            <textarea id = "losses" rows="4" cols="50"> 
                    "losses" 
                    </textarea>  (losses)

            <button type="button" id = "ruby" >Ruby</button>

            <button type="button" id = "diamond" >Diamond</button>

            <button type="button" id = "emerald" >Emerald</button>

            <button type="button" id = "bloodstone" >Bloodstone</button>
            
     
            <script src="http://code.jquery.com/jquery-3.3.1.js"
            integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
            crossorigin="anonymous"></script>
            <script src="assets/javaScript/crystal.js"></script>
     </body>
     </html>

如果totalScore超过targetNumber,则+1点损失并重置游戏如果totalScore小于targetNumber,则继续游戏如果totalScore等于targetNumber,则+1获胜并重置游戏

由于某些原因,比较和函数没有执行。

我不认为这是一个语法错误,我已经检查了代码几十次,看不出为什么它不能工作。这里我漏掉了什么?

EN

回答 2

Stack Overflow用户

发布于 2018-02-20 06:02:07

比较结果应该是这样的totalScore === targetNumber而不是totalScore = targetNumber

票数 1
EN

Stack Overflow用户

发布于 2018-02-20 06:09:41

此程序中有多个语法错误

代码语言:javascript
复制
$(document).ready(function() {

var totalScore = 0;


var targetNumber = Math.floor(Math.random() * 120) + 12;
$("#targetNumber").html(targetNumber);

var ruby = Math.floor(Math.random() * 10) + 1;
console.log(ruby);
var diamond = Math.floor(Math.random() * 10) + 1;
console.log(diamond);
var emerald = Math.floor(Math.random() * 10) + 1;
console.log(emerald);
var bloodstone = Math.floor(Math.random() * 10) + 1;
console.log(bloodstone);

$("#ruby").click(function() {
totalScore = totalScore + ruby;
console.log("totalScore");
$("#totalScore").html(totalScore + ruby);
});

$("#diamond").click(function() {
totalScore = totalScore + diamond;
console.log("totalScore");
$("#totalScore").html(totalScore + diamond);
});

$("#emerald").click(function() {
totalScore = totalScore + emerald;
console.log("totalScore");
$("#totalScore").html(totalScore + emerald);
});

$("#bloodstone").click(function() {
totalScore = totalScore + bloodstone;
console.log("totalScore");
$("#totalScore").html(totalScore + bloodstone);
});
});

$(document).ready(function() {

var totalScore = 0;
console.log(totalScore);
$("#totalScore").html(totalScore);

var ruby = Math.floor(Math.random() * 10) + 1; 
console.log(ruby);
var diamond = Math.floor(Math.random() * 10) + 1;
console.log(diamond);
var emerald = Math.floor(Math.random() * 10) + 1;
console.log(emerald);
var bloodstone = Math.floor(Math.random() * 10) + 1;
console.log(bloodstone);

var wins = 0;
$("#wins").html(wins)
var losses = 0;
$("#losses").html(losses)

$("#ruby").click(function() {
  totalScore = totalScore + ruby;
  console.log("totalScore");
  $("#totalScore").html(totalScore + ruby);
});

$("#diamond").click(function() {
  totalScore = totalScore + diamond;
  console.log("totalScore");
  $("#totalScore").html(totalScore + diamond);
});

$("#emerald").click(function() {
  totalScore = totalScore + emerald;
  console.log("totalScore");
  $("#totalScore").html(totalScore + emerald);
});

$("#bloodstone").click(function() {
  totalScore = totalScore + bloodstone;
  console.log("totalScore");
  $("#totalScore").html(totalScore + bloodstone);
});

if (totalScore < targetNumber); { // you are putting a 
//semi-colon after an if statement which creates an empty block
    $("#totalScore").html(totalScore);
    return(totalScore);
}
if (totalScore > targetNumber); { // <---- syntax error
// semi-colon after if statement
    $("#totalScore").html(totalScore);
    alert("you have exceeded the target number - Try again.");
    losses++;
    $("#losses").html(losses);
    start();
}
if (totalScore = targetNumber); { // <----you are using assignment as 
//test condition, returns true if target number is assigned to 
//total Score, probably meant === to compare two numbers
    $("#totalScore").html(totalScore);
    wins++;
    $("#wins").html(wins);
    alert("Nailed it!");
}})

从技术上讲,它们是逻辑错误,因为它们会导致程序中的意外行为,它们是有效的语法,但不是您想要的语法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48874815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档