oop - using functions inside javascript constructors -
i need write constructor object gives random value object's color property. wrote
var ghost = function() { // code goes here var num = math.floor(math.rand()*4); if(num === 0){ this.color = "white"; } else if(num === 1){ this.color = "yellow"; } else if(num === 2){ this.color = "purple"; } else if(num === 3){ this.color = "red"; } };
i error message test suite on code wars
typeerror: object #<object> has no method 'rand' @ new ghost @ test.describe
am not allowed use functions inside constructor or there test suite don't understand?
the name of function math.random
, not math.rand
.
to interpret error message:
typeerror: object #<object> has no method 'rand'
first try find object "rand" method trying call. in case, math. verify object in question indeed has method.
on unrelated note, selection code can simplified to:
var colours = ['white', 'yellow', 'purple', 'red']; this.color = colours[math.floor(math.random() * 4)];
Comments
Post a Comment