How to add a function to an object 12.7.1.2 of Week 4A Reading

I am trying to add the move function to each animal object. I get the function to work, creating a random number from 1-10. At first I tried passing that function into each animal but realized it would output the same number for all. Now, I am trying to write,

superChimpOne[“move”] = function(){let steps = Math.round(Math.random()*10); return (steps); };

When I check the results of this, I get

move: [Function]  

As my key/value output. I want the number to return.

I think this result is normal - that’s what I have too if I just print all the properties of my crew object. I’d suggest trying to use it on 1 of the animals to make sure it works - calling out the function on an animal & printing the result of the call-out & not just printing the animal object with its properties.

1 Like

I was hoping that would work.

This is my callout
for (item in superChimpOne){
console.log(item + superChimpOne[item]);
};

I get the usual output. And for the move function, it spits back out my function coding.

With this code you are iterating through your properties but you aren’t calling your function.
Try: console.log(superChimpOne.move());

1 Like

When adding new Property ‘astronuatID’ for ea animal assigning a # between 1 and 10 (including 10). How do you account for: However, no crew members should have the same ID? I could not figure this out.

Back to @Aimster original question:
With: superChimpOne.move = function() {return Math.floor(Math.random()*11)};
I also received: move: [Function] when printing: console.log(crew);
Solution showed:
let superChimpOne = {
XXXX
move: function () {return Math.floor(Math.random()*11)}
};
so I was confused. But I do get a random # between 0 and 10 when I call the function with console.log(superChimpOne.move()) as @StephNau recommended. So it works - but I could not figure out how to print it like solution shows above. Could anyone figure out how to print per solution?