Search code examples
javascriptarraysfunctionhasownproperty

Stuck on a javascript function for my game


so I'm creating a function to accept two arguments,

  1. the first parameter is the name of the person
  2. the second is how much they have already donated.

Each individual is assigned to an array depending on how much they are expected to donate. people in variable

  • a are expected to donate 6,
  • b should donate 5 and
  • c donate 4.

I want the function to return how many more donations should the individual make but here is the problem: no matter what name I input into the console log, it always assumes the person is from var a. Did I miss something here?

function findDonationRequirement(name, num){

var a = ["shad0vvfax","DarthPolekat","TheSchwartz",
"Johnnie SR91","Brodie", "HolyPaladin", "Kraven", "Dan Solo",
"Khorathian812", "KingWilliam"];

var b = ["Infantry0223","Ru Baruba Maral","Gray JediTim", "Moof Milker",
"Andain", "Cori Starfire", "Nassyy", "Roylas Trebla", "DarthPapirrin",
"MaximusGiganticus", "Wardai", "George3PO", "Revan2017", "Ravishing Dirk",
"dowi", "dogamidstwind", "SpinelessAce", "Devilscut88", "Dagez",
"The Buckster", "ddpf", "WoollyLemurToes", "Emeralthys Phantom  2060095",
"Lightnara1", "JoLy NYC",   "Ccube19", "Anectet Heat", "BigBadBoneDaddy",
"Grizzy", "Zanzibar", "Jesse", "BhMojo", "Kuu Raama", "Jay Fury", "N8Dog",
"Whiskytangofoxtrot"];

var c = ["KingofG0nd0r", "Chrome Cobra", "Bad Rongo", "TK421"];

if (a.hasOwnProperty(name)) {return 6 - num}

else if (b.hasOwnProperty(name)) {return 5 - num}

else if (c.hasOwnProperty(name)) {return 4 - num}

else "Not on list";}

console.log(findDonationRequirement("KingofG0nd0r", 3));

Solution

  • You should use indexOf()

    function findDonationRequirement(name, num) {
    
      var a = ["shad0vvfax", "DarthPolekat", "TheSchwartz",
        "Johnnie SR91", "Brodie", "HolyPaladin", "Kraven", "Dan Solo",
        "Khorathian812", "KingWilliam"
      ];
    
      var b = ["Infantry0223", "Ru Baruba Maral", "Gray JediTim", "Moof Milker",
        "Andain", "Cori Starfire", "Nassyy", "Roylas Trebla", "DarthPapirrin",
        "MaximusGiganticus", "Wardai", "George3PO", "Revan2017", "Ravishing Dirk",
        "dowi", "dogamidstwind", "SpinelessAce", "Devilscut88", "Dagez",
        "The Buckster", "ddpf", "WoollyLemurToes", "Emeralthys Phantom  2060095",
        "Lightnara1", "JoLy NYC", "Ccube19", "Anectet Heat", "BigBadBoneDaddy",
        "Grizzy", "Zanzibar", "Jesse", "BhMojo", "Kuu Raama", "Jay Fury", "N8Dog",
        "Whiskytangofoxtrot"
      ];
    
      var c = ["KingofG0nd0r", "Chrome Cobra", "Bad Rongo", "TK421"];
    
      if (a.indexOf(name)>-1) {
        return 6 - num
      } else if (b.indexOf(name)>-1) {
        return 5 - num
      } else if (c.indexOf(name)>-1) {
        return 4 - num
      } else {
        return "Not on list";
      }
    }
    
    console.log(findDonationRequirement("Ccube19", 3));
    console.log(findDonationRequirement("Chrome bra", 3));