Ask coding questions

← Back to all posts
When the player types in something how can I make it so it remembers what I typed?
seth08g (1)

Ok so in my game you can choose a weapon out of Sledgehammer, Mace, Sword or Daggers. I want to make it so when I choose one of these weapons, say Mace for example it remembers that I chose the Mace weapon. Please help me, Thanks..

Answered by codingjlu (481) [earned 5 cycles]
View Answer
Comments
hotnewtop
codingjlu (481)

On the top of your code, you could store a variable called weapon:

let weapon; // current value is undefined

And when the use chooses a weapon you could do something like:

weapon = "mace";

And later you can have like:

if(weapon === "mace")
    console.log("Your weapon is a ${weapon}!"); // ==> Your weapon is a mace!
seth08g (1)

@codingjlu
Thank you so MUCH this helped me be able to code the computer to remember the weapon the user picked, once again thx

ShluffyMonster (4)

I haven't tested it, but does https://replit.com/@ShluffyMonster/MagicKombat2#index.js work? the changes are at the top:
let weapon; --> if you need to, you can add = "none" before the semicolon
after each case:
weapon = case; --> you could manually add it so if case is daggers, under it you will type daggers rather then case into that variable

Avirah (0)

You need to 'break' out of the case once it is executed (do this for all of your cases).

Also when writing 'switch' statements, you should have a 'default' (ie. case 1, case 2, case 3..., default), in the off chance that none of your cases are met.