19 - More kinds of methods


#1

I passed the lesson, but I'd also like to choose some values for the side of the square to calculate the area and the perimeter.

Here's the code:

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();

I tried adding square.sideLength(somenumber), but it didn't work, can anyone help me? How can I choose values for the sideLength? I know I could just change the 6 in the second line, but in the other exercises I was able to change the value by adding another line in the end, without changing the previous one, is it possible here?


#2

Have you tried doing the same thing here..? And if it's not doing what you want, then what did you try and what effect did you get and what happened instead?


#3

I tried to add square.sideLength(3), but I got TypeError: square.sideLength is not a function. Why does it work n the second line, but not in the end of the code? Then I tried square.sideLength = 3, but it made absolutely no difference.


#4

If that's what you did earlier, then you would have to have made that function as well.

Not sure what you did to test that, but it does make difference when added to your code above, did you compute the area and then change the side length? You'll have to change it first for there to be noticeable effect.

I could just have told you how to set properties, indeed you could have found it on google, but that would be missing/ignoring the bigger lesson to be learned here, which is the ability to see how something was done somewhere else and re-produce it again, because you've got everything you need to do that! But it went wrong somewhere.