-3

Basically I have three values.

Each of those values is specified in a variable.

In the if statement I would like to state all these 3 values to be true in order for it to perform the if statement.

For example: val == 'Millennium Wheel' and val2 = 'Palace of Westminster' and then comes val3, all of these should be stated to be true before the if statement is applied.

Code is given below, thanks for your help.

function checkanagram() {
  var val = $("#anagramtext").val();
  var val2 = $("#anagramtext2").val();
  var val3 = $("#anagramtext3").val();
  if (val == 'Millennium Wheel') {
    alert('Correct Answer!');
  } else {
    alert('Incorrect answer, try again');
  }
}
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
Azzie
  • 49
  • 7
  • Possible duplicate of [Whats the difference between & and && in JavaScript?](https://stackoverflow.com/questions/7310109/whats-the-difference-between-and-in-javascript) – Fred Gandt Jul 23 '17 at 23:27
  • 1
    This is covered in any basic tutorial, and google search. Please utilize these great utilities :) – Sterling Archer Jul 23 '17 at 23:36

3 Answers3

0

[UPDATED]

Just use the logical operator && to require all 3 terms to be true. For example:

if (val == 'Millennium Wheel' && val2 == 'Palace of Westminster' && val3 == 'Buckingham Palace') {
    ....
}
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • There are 2 [equality operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators). I had accidentally used strict equaiity (`===`) for `val3`. Try using `==` instead if `val3` needs to be converted to a string before comparison. – cybersam Jul 24 '17 at 02:25
0

It sounds like you're looking for the bitwise Logical AND operator (&&).

In the following example, the correct answer will only be triggered when all three inputs have the value 'Millenium Wheel':

function checkanagram() {
  var val = $("#anagramtext").val();
  var val2 = $("#anagramtext2").val();
  var val3 = $("#anagramtext3").val();
  if (val == 'Millennium Wheel' && val2 == val && val3 == val) {
    alert('Correct Answer!');
  } else {
    alert('Incorrect answer, try again');
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="anagramtext"><br />
<input id="anagramtext2"><br />
<input id="anagramtext3"><br />
<button onclick="checkanagram()">Go!</button>

If you want to make it so that val must be Millennium Wheel while val2 must be Palace of Westminster, and that val3 can be anything, you can simply specify these requirements in the AND statement:

function checkanagram() {
  var val = $("#anagramtext").val();
  var val2 = $("#anagramtext2").val();
  var val3 = $("#anagramtext3").val();
  if (val == 'Millennium Wheel' && val2 == 'Palace of Westminster' && val3) {
    alert('Correct Answer!');
  } else {
    alert('Incorrect answer, try again');
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="anagramtext"><br />
<input id="anagramtext2"><br />
<input id="anagramtext3"><br />
<button onclick="checkanagram()">Go!</button>

Note that the absence of any equality for val3 simply checks that anything is entered in that input.

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
0

You need to use the logical AND operator (&&) between each of the conditions:

function checkanagram(){
    var val = $("#anagramtext").val();
    var val2 = $("#anagramtext2").val();
    var val3 = $("#anagramtext3").val();

    if ((val == 'Millennium Wheel') && 
        (val2 == 'Palace of Westminster') && 
        (val3 == 'Wheel mum nine ill')) 
    {
        alert ('Correct Answer!');
    } else {
         alert ('Incorrect answer, try again');
    } 
}
Stephen R. Smith
  • 3,310
  • 1
  • 25
  • 41