0

I'm brand new to Javascript.

I'm playing around with some code here and can't get my if to work, but my else works.

The problem is with (solved == true), it's not working. Not sure what I'm doing wrong here.

 <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>
Sdbseo
  • 15
  • 2
  • 3
    You're declaring `solved` inside a function, so it's scoped to that function and inaccessible elsewhere. Also your `if` statement sets the variable to true, instead of testing it; use `==` or `===` instead of `=`. (Lastly -- this won't break anything, but it's unnecessary to use separate ` – Daniel Beck Dec 24 '17 at 19:56
  • Possible duplicate of [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Sebastian Simon Dec 24 '17 at 19:56

5 Answers5

2

You have a global variable solved and you use a local variable in the function where the global variable does not change the value.

You need just the global variable.

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)
}

Then you need to check the variable without an assignment.

if (solved === true) {

or shorter use the value directly with a truthy value

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>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You have solved = true but you should change it for solved == true. And like someone said it is inaccessible.

Joe
  • 41,484
  • 20
  • 104
  • 125
Wiktor Dębski
  • 188
  • 3
  • 16
0

You have a missing var in you upper definition of solved = true. Also as other have mentioned, test a condition using double equals solved == true Or triple =, as some has suggested...

niCk cAMel
  • 869
  • 1
  • 10
  • 26
0

You're declaring solved inside a function, so it's scoped to that function and inaccessible elsewhere. Also your if statement sets the variable to true, instead of testing it; use == or === instead of =.

(Lastly -- this won't break anything, but it's unnecessary to use separate tags for each function; you can put all that stuff in a single script block.)

Here's a version of your code with those issues corrected:

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>
Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
0

The problem is that you create multiple vars named solved. For instance in this function

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

The var solved will only be visible inside the function solvedTrue(). So it is NOT the same var you created at the top with

solved = true;

You need to end lines of code with a semicolon which you failed to do throughout your code.

Not related to your problem but you need to know that it isn't necessary to repeatedly use the <script> tag. Here's a revised version of your 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>
DFriend
  • 8,869
  • 1
  • 13
  • 26