Seeking assistance in resolving issues with the functionality of my script.
function CatFactory(cat) // Cat constructor
{
for (y in cats)
{
if (cats[y].color == cat.color)
{return false;} // return false if already in the array
}
return cat; // otherwise, return the cat object itself
}
function IPreferUniqueCats(cat)
{
if (new CatFactory(cat))
{
cats[y].push(cat);
}
}
However, when (cats[y].color == cat.color)
evaluates to true
, the new CatFactory(cat)
will be an empty constructor instead of a desired FALSE
.
The primary issue is that I am able to add two cats with the same color to my cats array. This makes me quite disappointed.
Could you please provide guidance on how to properly utilize this script?