0

I am trying to compare two different variables with each other and execute certain code if they match. The variables are: userInput and commandOne

Firstly, a button is pressed to call myTest().

function myTest() {
   userInput = document.getElementById("runBox").value;
   testInput();
}

The function gathers text from an input box on my page and stores it in a variable named userInput. It then calls my next function...

function testInput() {
  if (userInput = commandOne) {
     document.getElementById("demo").innerHTML += "<br>executed!";
  }else {
     alert("this is an alert message");
  }
}

This part is designed to test if userInput matches the variable named commandOne. (commandOne's value is currently set to "ip").

If it does match, it will add text (executed!) to a paragraph with the "demo" ID. If it does not match, it will alert the user in an alert box.

My problem is that the variables do not seem to be comparing. No matter what the user puts into userInput the text (executed!) is always outputted to my paragraph. It appears that the browser thinks they are matching when they are not.

3
  • 2
    = is assignment operator, for comparison use either == or === Commented Mar 27, 2018 at 4:01
  • OMG thank you so much! I cant believe I over looked something as small as that! thx Commented Mar 27, 2018 at 4:02
  • You don't compare variables, you compare their values. ;-) This should be closed as off topic, it's a simple typo. Commented Mar 27, 2018 at 4:25

3 Answers 3

2

You missed your operator in the if statement.

if (userInput == commandOne)

== compares value

if (userInput === commandOne)

=== compares values and data types.

Sign up to request clarification or add additional context in comments.

Comments

0

You had used the wrong operator. You must use == sign instead of a single = sign.

function testInput() {
  if (userInput == commandOne) {
     document.getElementById("demo").innerHTML += "<br />executed!";
  } else {
     alert("This is an alert message");
  }
}

A single = sign (=) means that the value on the left side of the sign gets the same value as on the right side of the sign.

Double= sign (==) means that two values on the each side of the sign are equal.

Triple = sign (===) means that both the values are equal and of the same type.

Click here for more information.

Comments

0

As mentioned - you have the wrong operator - however i just wanted to show an alternative to the logic: - the ternary operator - makes it much nicer and cleaner to read and reduces the if / else markup.

Incidentally - given the code provided - you don't even need to call teh second function - the entire thing can be done in one function. Also - if the alert is purely to demonstrate the "else" outcome - you should investigae the use of the console.log() - its bettter for debugging.

To explain the ternary operator - the condition to be met is written first (note that there is no "if" preceding it. Following it - use the "?" charcter to give an action if hte comparison is true and use a ":" character for if the outcome is false.

Note that I always write the ternary operators on the three lines as i have done here - I find it easier to read - it can all be written on one line - personal preference.

And last thing - no ";" at the end of the "true" portion of the statemtn - it is all one expression that I happen to have written over three lines.

function testInput() {
  userInput == commandOne
    ? document.getElementById("demo").innerHTML += "<br>executed!"
    : alert("this is an alert message");
}

5 Comments

Ok thanks, I will give that a go. Also, alert() is temporary- it will be replaced with more code!
This trick doesn't work well for more than one line of code per block, and it doesn't save that much over an if/else in terms of real estate, and it makes it less obvious what your code is doing.
If I was to do: var voteable = (age < 18) ? "Too young":"Old enough"; Does that mean (age < 18) counts as the value AND condition?
Always 3 lines? Even for return foo? bar : fum? Also ? : is formally called the conditional operator, which is a compound operator and a ternary operator.
geez guys - i said it was a personal preference - i didn't invent ternary operators - i just use them - - to respond to above - you can easily have more than one line in each block (wrap all with brackets and put comma's between different commands) and i think its easy to read - but thats me. and yes - there are exceptions to when I might use one line versus 3 - I personally would use 3 for demonhunters example and 1 line only for RobG's example. Although demonhunters example is short - the use of three lines seaprates the portions so you don't have to break aprart the true and false strings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.