Alexander Elsano Alexander Elsano - 1 month ago 9
Javascript Question

if/else statement confusion

I'm thinking about making a small semi-game where you respond to a mysterious void about things. The user sees a question on-screen, types a response in the textbox and clicks the reply button, and the script replies.

My main problem is to understand the logic behind all this.

For example:

Do you like apples?
A1:Yes
A2:No

if(yes){
say this
} else if(no){
say that
}


What would be the appropriate syntax in this case? Because i'm really really confused right now, it may seem like a very noob-question, but jQuery is getting on my nerves.

Answer

var input = document.querySelector('input'),
  output = document.getElementById('output');

input.onkeyup = function(event) {
  switch (this.value.toLowerCase()) {
    case 'chicken':
      output.innerText = "I like chicken too";
      break;
    case 'meat':
      output.innerText = "mmmmm, meat!!";
      break;
    default:
      break;
  }
};
<form>
  <input type="text" />
  <div id="output">Huh?</div>
</form>

You could also just do this

var isYes = true;
(isYes) ? /* Do this */ : /* Do this */ ;

Saves you lines of code

Edit: As you had requested I have created a snippet. Try it out, type in chicken/meat

Comments