-3

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));
SSD
  • 1,373
  • 2
  • 13
  • 20
George Salamanca
  • 180
  • 1
  • 3
  • 11

2 Answers2

1

Instead of a.hasOwnProperty(name) use a.indexOf(name) > 0

hasOwnProperty is an Object method to look up the property with the name name

indexOf is an Array method to look up a literal or object's position inside an Array. When it is not found it returns -1. So play with its value to figure out if it exists in the a Array.

more on indexOf here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

Abderrahmane TAHRI JOUTI
  • 3,514
  • 2
  • 26
  • 43
0

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));