如果(Var == True){做这个! }

时间:2017-12-24 19:54:14

标签: javascript

我是Javascript的新手。

我在这里玩一些代码并且不能让我的工作,但我的其他工作。

问题在于(已解决== true),它无效。不知道我在这里做错了什么。

 <script>
        solved = true
    </script>

    <script type="text/javascript">
        function solvedTrue(){
            var solved = true
            console.log("Solved is now " + solved)
    }
    </script>

    <script type="text/javascript">
        function solvedFalse(){
            var solved = false
            console.log("Solved is now " + solved)
    }
    </script>

    <script type="text/javascript">
            function checkStatus(){
            console.log("Currently it's " + solved)
    }
    </script>

<script type="text/javascript">
            function tester(){
                if (solved = true) {
                alert('it worked') }
                else {
                    alert('Did not work')
</script>

    <button onclick="tester()">If/Else Test</button>
    <button onclick="solvedTrue()">True</button>
    <button onclick="solvedFalse()">False</button>
    <button onclick="checkStatus()">What's the Status?</button>

5 个答案:

答案 0 :(得分:2)

您有一个全局变量solved,并且您在函数中使用局部变量,其中全局变量不会更改该值。

您只需要全局变量。

var solved = true; // use var for declaration a variable

function solvedTrue(){
    solved = true; // no var, because you want to take the global var
    console.log("Solved is now " + solved)
}

然后你需要在没有作业的情况下检查变量。

if (solved === true) {

或更短的值直接使用truthy

if (solved) {

function solvedTrue() {
    solved = true;
    console.log("Solved is now " + solved);
}

function solvedFalse() {
    solved = false;
    console.log("Solved is now " + solved);
}

function checkStatus() {
    console.log("Currently it's " + solved);
}

function tester() {
    if (solved) {
        alert('it worked');
    } else {
        alert('Did not work');
    }
}

var solved = true;
<button onclick="tester()">If/Else Test</button>
<button onclick="solvedTrue()">True</button>
<button onclick="solvedFalse()">False</button>
<button onclick="checkStatus()">What's the Status?</button>

答案 1 :(得分:1)

你已经解决了= true但是你应该改变它来解决== true。就像有人说它无法进入。

答案 2 :(得分:0)

var的上限定义中缺少solved = true。正如其他人提到的那样,使用double等于solved == true测试条件 或者三倍=,正如一些人所建议的那样......

答案 3 :(得分:0)

你在一个函数中声明已经解决了,因此它的作用域是该函数,并且在其他地方无法访问。你的if语句也将变量设置为true,而不是测试它;使用=====代替=

(最后 - 这不会破坏任何内容,但不必为每个函数使用单独的标记;您可以将所有内容放在一个脚本块中。)

以下是修正了这些问题的代码版本:

var solved = true // var here makes it global.  This is not strictly necessary, but it's good practice.

function solvedTrue() {
  solved = true   // don't use "var" here; it would create a separate variable scoped to this function
  console.log("Solved is now " + solved)
}

function solvedFalse() {
  solved = false
  console.log("Solved is now " + solved)
}

function checkStatus() {
  console.log("Currently it's " + solved)
}

function tester() {
  if (solved) { // or use   if (solved === true).   solved=true sets the value instead of testing it.
    alert('true')
  } else {
    alert('false')
  }
}
<button onclick="tester()">If/Else Test</button>
<button onclick="solvedTrue()">True</button>
<button onclick="solvedFalse()">False</button>
<button onclick="checkStatus()">What's the Status?</button>

答案 4 :(得分:0)

问题是您创建了多个名为solved的变量。例如在这个函数中

function solvedTrue(){
        var solved = true
        console.log("Solved is now " + solved)
}

变量solved仅在函数solvedTrue()中可见。所以它与你在

顶部创建的变量不同
solved = true;

您需要使用分号结束代码行,但在整个代码中都没有这样做。

与您的问题无关,但您需要知道重复使用<script>标记是不必要的。这是您脚本的修订版本。

<script>
var solved = true; //this will be visible to all functions

function solvedTrue() {
    solved = true;
    console.log("Solved is now " + solved);
}


function solvedFalse() {
    solved = false;
    console.log("Solved is now " + solved);
}

function checkStatus() {
    console.log("Currently it's " + solved);
}

function tester() {
    if (solved === true) //use equality not assignment
    {
        alert('it worked');
    } 
    else
    {
        alert('Did not work');
    }
}
</script>