Please help me to fix the working of my script.
function KittyFactory(kitty) // kitty constructor
{
for (x in kitties)
{
if (kitties[x].color == kitty.color)
{return false;} // if already in the array return false
}
return kitty; // else return the object itself
}
function iPreferDifferentKitties(kitty)
{
if (new KittyFactory(kitty))
{
kitties[x].push(kitty);
}
}
But if the (kitties[x].color == kitty.color) is true the new KittyFactory(kitty) will be an empty constructor (the function itself) instead of a huge FALSE what i want.
My problem is basicly i can put to my kitties array two kitties with the same color. 🙁 Makes me sad.
Could you help me please how should i use it?
You can’t return
falsefrom a constructor. When you invoke a function with thenewoperator, the return value must be an object. If you try to return something else, it behaves as if there were noreturnstatement (and the newly constructed object is returned by default).