I think I'm being a bit over ambious here but could someone help me out?


#1

I'm trying to build the code so that it accepts multiple inputs and responds i.e. if you type in "push button" you'll get an answer but if you just type "push" the program will ask "push what" and if they say button it will give the same answer. For some reason it doesn't register the second input or the two word input.

var user = prompt("You find yourself in the cargo bay of a Vogon Constructor. In front of you is a large machine with a slot at the bottom and a friendly-looking red button. What would you like to do?").toUpperCase();

switch(user){
case ("PUSH" || "BUTTON"):
if("PUSH"){
var pushWhat = prompt("Push...what exactly?");
if(user === "PUSH" && pushWhat === "BUTTON"){
console.log("A small yellow fish pops out of the slot at high speed, sails across the room and falls down a drain in the floor.");
}else{
console.log("I don't think pushing that will help.");
}
}else if("BUTTON"){
var buttonWhat = prompt("Yeeees, do what to the button?");
if(user === "BUTTON" && buttonWhat === "PUSH"){
console.log("A small yellow fish pops out of the slot at high speed, sails across the room and falls down a drain in the floor.");
}else{
console.log("Why would you do that to a button?");
}
}else{
console.log("A small yellow fish pops out of the slot at high speed, sails across the room and falls down a drain in the floor.");
}
break;
case ("USE"||"TOWEL"):
if("USE"){
var useWhat = prompt("Use what?");
if(user === "USE" && useWhat === "TOWEL"){
console.log("While very important and useful, that doesn't really help you in this circumstance. Nice try though.");
}else{
console.log("I don't using that will help here.");
}
}else if ("TOWEL"){
var towelWhat = prompt("And, what are you going to do with your towel?");
if(user === "TOWEL" && towelWhat === "USE"){
console.log("While very important and useful, that doesn't really help you in this circumstance. Nice try though.");
}else{
console.log("What a horrible thing to do to a towel");
}
}else{
console.log("While very important and useful, that doesn't really help you in this circumstance. Nice try though.");
}
break;
case ('CONSULT' && 'GUIDE'):
console.log("As mind-bogglingly informative and comprehensive as the Hitchhicker's Guide to the Gaxaly is, it does require that the user be somewhat less vague with they're request then that. The authors apologise and promise to improve upon this in the future.");
break;
case ('DRINK'&&'TEA'):
console.log("You do not have tea at the moment. Once the business with this machine concluded you should really do something about that over sight.");
break;
case "LOOK":
console.log("You see a large machine with a slot at the bottom and a friendly-looking red button. You ever get the feeling of deja vu? Anyway, what would you like to do?");
break;
case "PUT TOWEL ON DRAIN":
console.log("You cover the drain with your towel. No small, leach-like, language translating fish a sliping down there! Please ignore that oddly specific statement. Now what would you like to do?");
break;
default:
console.log("Sorry, I don't recognise what you just said. Either you're speaking jibberish or another language I don't understand. Perhaps the machine can help with that.");
};

As you might be able to tell it's based on the Hitchhiker's guide to the Galaxy adventure game.


#2

You only called .toUpperCase() on your very first prompt, you need to call it on all of your prompts for your case checks to be consistent.

It would also be a really good idea to compare the var value from user with the result.

As for the switch not registering both commands, well your switch case is only ever checking for two separate strings, however the prompt is creating a single string, with a space between them. There is no check for "PUSH BUTTON", and if you do a check for "PUSH" && "BUTTON" you have to remember if they are typing "PUSH BUTTON" there is a space between them, so technically you would be checking for something like "PUSH " && "BUTTON.


#3

Thanks for that. I thought that having the case: "PUSH" || "BUTTON" would have it check for both and finding both true would print the else on line 19 of the if/else. i.e. console.log("A small yellow fish pops out of the slot at high speed, sails across the room and falls down a drain in the floor.");

If I want it to do that do I have create a separate case for "PUSH " && "BUTTON"?


#4

You can test it yourself by typing in the full string "push button" and even that won't be accepted, because it's looking for

is "push button" === "push"
or
is "push button === "button"

It's also only looking for either one to be true. If you wanted this case to work, you'd have to split up the user answer into an array and check each word, or you could use a standard javascript string function to search the user's response for the input you want.

Javascript string search()
http://www.w3schools.com/jsref/jsref_search.asp

If you look here, you're basically doing what I just mentioned already, only through extra steps, and possibly some errors. The only way this works if the user enter's either push or button alone on the initial prompt, you then ask them to push what and store that value in a separate string.
So you're checking two separate strings for individual values. The only problem with what you already had in this sense, is that you were comparing only for all UpperCase values throughout your script, when you forgot to call .toUpperCase() on all of the prompts.


#5

A good way to get through this is to make your own function, and call that whenever you want to ask for user input. This will take a bit of planning to make this a all encompasing function, but would save you a great deal of trouble, and clean your code up tremendously.

// Let's define a function to handle all user inputs
// It'll handle all variables for the function in parameters
// userInp will take the users answer
// checkInp will be what you want it to equal
// passDial & failDial will be your console returns if they pass or fail
function get_input( userInp, checkInp, passDial, failDial ) {
// trim() removes whitespace on both sides
   var Inp = trim(userInp)
// .split() sorts a string into array
   Inp = Inp.split(" ")
 for(i=0; i<Inp.length; i++) {
    //check each array index for your checkInp comparison }
// write some if statement if found

get_input(  prompt("Push...what exactly?"), "PUSH", "A small yellow . . .", "I don't think pushing that...");

#6

Would this be in addition to the already existing code or starting fresh with your code as the base.


#7

This would simply be doing what your if/else statements in each case are already doing. Take a user input, clean it up, check it with your expected input, if it matches return dialogue, if it fails, return fail dialogue.

the four parameters are user input, your expected input, and pass/fail dialogue It allows you to achieve the same thing without as many if else statements repeating.

I'm not going to rewrite your entire code, or tell you to use exactly what I did, but it gives you an idea how you can make it easier to check conditions without as much clutter. It would also allow you to swap out your conditions, like if you wanted different success or fail dialogue, or to check for different values. It's all taking place in one function.


#8

Cheers thanks. I'll play around with the code you given me there and see if I can get it working for what I want. Thanks again for the help.


#10