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

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -