1

I'm trying to make a simple 'Choose Your Adventure!' game, and I seem to have run into a problem. I don't know how to target certain values of this multi-dimensional array I made. I made a 'dealer/trader' and have his items on sale like this.

var dealer = [
  [
    {type: "weapon", cost: 250, name: "Claymore"},
    {type: "weapon", cost: 75, name: "Dagger"},
    {type: "weapon", cost: 350, name: "Magic Staff"},
    {type: "weapon", cost: 150, name: "Sword"},
    {type: "weapon", cost: 125, name: "Bow"},
    {type: "weapon", cost: 125, name: "Crossbow"},
    {type: "weapon", cost: 5, name: "Arrow"},
    {type: "weapon", cost: 15, name: "Bolt"}
  ],
  [
    {type: "clothing", slot: "head", name: "Helmet"},
    {type: "clothing", slot: "head", name: "Hood"},
    {type: "clothing", slot: "chest", name: "Chestplate"},
    {type: "clothing", slot: "chest", name: "Tunic"},
    {type: "clothing", slot: "chest", name: "Robe"},
    {type: "clothing", slot: "leggings", name: "Legplates"},
    {type: "clothing", slot: "leggings", name: "Leggings"},
    {type: "clothing", slot: "leggings", slot: "Undergarments"},
    {type: "clothing", slot: "feet", name: "Boots"},
    {type: "clothing", slot: "feet", name: "Armored Boots"}
  ]
]

And I have a function that operates the dealer, such as buying an item, I don't know how to target certain values/arrays. This is what I THINK will work.

function merchant() = {
    var armor = function(slot, name, material)  {
        if(dealer[2].slot === "feet" && dealer[2].name = "Boots"}
            money -= 10;
        }
    }
}

That should target the second array of the clothing and look for the slot feet and name of boots, right?

5
  • Looks like invalid javascript to me, there's an equal sign in the first function decleration, and armor is local to the function, how to you intend to call it ? Commented Feb 6, 2015 at 18:18
  • 1
    Also, arrays are zero indexed, so the second one would be [1] Commented Feb 6, 2015 at 18:19
  • You can pass the dealer id into the armor function. Also, indexing begins at zero (0). Commented Feb 6, 2015 at 18:21
  • 1
    You are indexing the dealer array (incorrectly) to get (presumably the second) dealer, but that returns another array. To get things in that array you will need to index again. So dealer[1][8].slot === "feet". But this might be better rearranged as an object keyed off of one of those properties, but it's hard to say without more context. Commented Feb 6, 2015 at 18:24
  • The items in the dealer array should be objects and they should contain items. var dealers = [ { name: 'Weapons Dealer', items: [ { type:'weapon', cost: 75, name: 'Dagger' }]}, ... ] Commented Feb 6, 2015 at 18:26

1 Answer 1

1

I would make the merchants array contain merchant objects. This allows you to give more information about a merchant, including items. I have two find methods. The first takes static arguments, the second allows for key-value parameters.

Note: I added a cost field to your boots, as this was somehow related to your example.

var merchants = [{
  name : 'Weapons Merchant',
  items : [
    {type: "weapon", cost: 250, name: "Claymore"},
    {type: "weapon", cost: 75,  name: "Dagger"},
    {type: "weapon", cost: 350, name: "Magic Staff"},
    {type: "weapon", cost: 150, name: "Sword"},
    {type: "weapon", cost: 125, name: "Bow"},
    {type: "weapon", cost: 125, name: "Crossbow"},
    {type: "weapon", cost: 5,   name: "Arrow"},
    {type: "weapon", cost: 15,  name: "Bolt"}
  ]
}, {
  name : 'Armor Merchant',
  items : [
    {type: "clothing", slot: "head",     name: "Helmet"},
    {type: "clothing", slot: "head",     name: "Hood"},
    {type: "clothing", slot: "chest",    name: "Chestplate"},
    {type: "clothing", slot: "chest",    name: "Tunic"},
    {type: "clothing", slot: "chest",    name: "Robe"},
    {type: "clothing", slot: "leggings", name: "Legplates"},
    {type: "clothing", slot: "leggings", name: "Leggings"},
    {type: "clothing", slot: "leggings", name: "Undergarments"},
    {type: "clothing", slot: "feet",     name: "Boots",       cost : 10},
    {type: "clothing", slot: "feet",     name: "Armored Boots"}
  ]
}];

function main() {
  // Static approach
  var armorMerchant = findMerchant(merchants, 'Armor Merchant');
  var boots = findItem(armorMerchant, 'clothing', 'Boots');
  print('Boots: $' + boots.cost);

  // Dynamic approach
  var weaponsMerchant = findMerchant(merchants, 'Weapons Merchant');
  var dagger = findWithParams(weaponsMerchant.items, {type:"weapon",name:"Dagger"});
  print('Dagger: $' + dagger.cost);
}

function findMerchant(merchants, name) {
  return find(merchants, function(merchant) {
    return merchant.name === name;
  });
}

function findItem(merchant, type, name) {
  return find(merchant.items, function(item) {
    return item.type === type && item.name === name;
  });
}

function findWithParams(arr, parameters) {
  return find(arr, function(item) {
    for (var parameter in parameters) {
      if (item[parameter] !== parameters[parameter]) {
        return false;
      }
      return true;
    }        
  });
}

function find(arr, predicateFn) {
  var found = null;
  arr.forEach(function(item, index, items) {
    if (predicateFn.apply(undefined, arguments)) {
      found = item;
      return true;
    }
    return false;
  });
  return found;
}

function print(text) {
  document.getElementById('out').innerHTML += text + '<br />';
}

main();
<div id="out"></div>

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.