0

Pretty sure that my title is misleading... Oh well. So basically, I'm trying to create a game and I have a bunch of arrays with names like ItemsInG5Array, ItemsInB2Array where G5 and B2 are nodes of the map. So, what I'm trying to do is to create a function that will give you a list of items in one specific node where the player is.

function options(location) 
locationPickups: while(true) {
pick = prompt("There are few items laying around.\n" + itemsInG6Array + "\nWould ou like to pick something up?");
...

Where location is the name of the node. Is there anything simple I can put instead of ItemsInG6Array to make it dynamic, to make it change depending on the location variable. ItemsIn(location)Array

1

1 Answer 1

1

The typical solution here is to have an object with keys, and then dynamically pick one of those keys.

const example = {
  itemsInG6Array: [],
  itemsInB2Array: [],
}

function options(location) {
  const key = 'itemsIn' + location + 'Array';
  const items = example[key];
  const pick = prompt("There are few items laying around.\n" + items + "\nWould ou like to pick something up?");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.