Skip to content
Sign upLog in
← Back to Community

How do I add a "Invalid Argument, Please Try Again" Commands to my code?

Profile icon
Hamburgr

In my game, I am trying to add a command that if the user doesn't write a valid argument, it will replay the code. Currently, for the main startup for the code, it has two options attack and run. I am trying to make it so if the user puts in an argument that is not one of those two, it will say Invalid Argument, Please enter a valid option. Can anyone help me with this?

@Coder100
If you haven't noticed, this is clearly a remake of one of your game jam entries in a different language. This was made solely for learning purposes.

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
Profile icon
LandofStorie
Profile icon
Hamburgr
Comments
hotnewtop
Profile icon
Coder100

Ok, so I would recommend you doing something like:

console.log('A ' + enemyType + ' approaches!'); console.log('1. Attack') console.log('2. Run') var userOption = prompt('What would you like to do?: '); switch (userOption) { case "run": // code break; case "attack": // code break; default: console.log("invalid"); break; }

You can read up on it here

Profile icon
Coder100

and no I am honored that people take inspiration :)

Profile icon
Coder100

ok looking on to it

Profile icon
[deleted]

https://repl.it/@idrgplayer/MildWorseApplet#index.js
Try my code, is it supposed to be like this.

Profile icon
[deleted]

@idrgplayer
if yes then pls mark my answer correct. :)

Profile icon
RYANTADIPARTHI

Put an else

like this:

else { document.write("not valid") }
Profile icon
Hamburgr

@RYANTADIPARTHI
Not working :(

Profile icon
RYANTADIPARTHI

@Hamburgr
put the else with an if statement.