How To Make An Error Message When Someone Enters An Invalid Command
This link is to a Thimble project I'm working on using JS and Jquery. I'm making a text-based game, and I wondered if someone could help me make an error message appear so that whe
Solution 1:
With zero code and zero idea how your app actually works, this is the best I can give you.
// Create an array of acceptable commandsvar commands = [
"command1",
"command2",
"command3"
];
// Get the command typed into the gamevar commandTyped = $("#command-line").val();
// Check if the command typed was in the array of acceptable commands// If not do somethingif( $.inArray( commandTyped, commands) == -1 ) {
alert("That command doesn't exist");
}
Solution 2:
var commands = [
"command1",
"command2",
"command3"
];
// Get the command typed into the gamevar commandTyped = $("#command-line").val();
if (commands.indexOf(commandTyped) === -1) {
alert('Invalid command!');
}
indexOf will ensure browser compatibility. It returns -1 when a list doesn't contain an item.
Alternatively, you could use includes()
if (commands.includes(commandTyped)) { ...
Post a Comment for "How To Make An Error Message When Someone Enters An Invalid Command"