Number 19. More kinds of methods


#1

var square = new Object();
square.sideLength = 6;
square.calcPerimeter = function() {
return this.sideLength * 4;
};

square.calcArea = function() {
return this.sidelength * this.sidelength;
};

var p = square.calcPerimeter();
var a = square.calcArea();

That is my code right there. An error shows up saying: "Your method failed when sideLength was 0 where it returned NaN instead of 0.


#2

Ok Problem identified. You just had a mispell there. Here:

Its suppose to be this.sideLength

Dont forget always camel case in programming. Very essential in programming!


#3

Thank you very much for your help! :smiley:


#4

square.calcArea = function(){
if (this.sideLength === 0){
return 0
} else if(this.sideLength === 1){
return 1
}else{
return this.sideLength * this.sideLength}};


#5

Damn! I was stuck on this, the serif font of 'l' confused me and I didn't realise the 'l' was not in capital! :smiley: