Alexander Elsano Alexander Elsano - 1 year ago 102
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?

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 Source

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";
    case 'meat':
      output.innerText = "mmmmm, meat!!";
  <input type="text" />
  <div id="output">Huh?</div>

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download