Object method


#1

please help..(on lesson 19),
can u look at this and see why gives an error message please

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

square.calcArea(7);
square.calcPerimeter(5);
error:Oops, try again. Your method failed when sideLength was 0 where it returned NaN instead of 0


#2

Hi @reefoo

Try with console.log (because it's the last return who will be display on the console)

var square = new Object();
square.sideLength = 6;

square.calcPerimeter = function(){
    console.log(this.sideLength * 4);
};

square.calcArea = function(){
    console.log(this.sideLength * this.sideLength);
};

square.calcPerimeter();
square.calcArea();

#3

i have tried that now and got this [Oops, try again. Your method failed when sideLength was 0 where it returned undefined instead of 0]


#4

I really don't know what's wrong, what's your exercise instructions ?


#5

It's supposed to be return. Look at my post called "Console.log vs Return" for more info. @reefoo, you have a problem that is very easy to miss. Look at this:

You are consistently misspelling "Length" as "Lenth". Still doesn't work? Let me know and I'll research.


#6

As long as you consistently misspell, it with will work, as long as you don't misspell compiler or library built-in names.


#7

Yes, but Codecademy probably would not accept it even though it worked. And even so, part of it was already there and in it it was spelled "Length", not "Lenth".


#8

you failed because you misspell Length and lack of use return keyword instead of console.log().

var square = new Object();
square.sideLength = 6;
square.calcPerimeter = function() {
  return this.sideLength * 4;
};
// help us define an area method here
square.calcArea = function() {
    return this.sideLength * this.sideLength;
}
square.calcPerimeter();
square.calcArea();