Confused With Changing square.sideLength Property & Variable Positioning


#1


https://www.codecademy.com/en/courses/spencer-sandbox/2/7?curriculum_id=506324b3a7dffd00020bf661


I'm trying to set a new square.sideLength value just to play around a little with the existing exercise and build off what I learn earlier.

The current position of the p and a variables in the code only take into account the original length value. If I put the variables at the very beginning, they only take into account the newest value. Could anyone explain how to make these variables use the most current value each time they are used in the console.log?

This is what is logged to the console with the current code:

Perimeter is: 24
Area is: 36
Setting square.sideLength to: 15
Current square.sideLength is: 15
Perimeter is: 24
Area is: 36


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;  
};

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

console.log("Perimeter is: " + p);
console.log("Area is: " + a);

// -----The extra code I added is below-----

var setLength = function(newLength) {
    return this.sideLength = newLength;
};

square.setLength = setLength;

console.log("Setting square.sideLength to: " + square.setLength(15));
console.log("Current square.sideLength is: " + square.sideLength);
console.log("Perimeter is: " + p);
console.log("Area is: " + a);


#2

There's a LOT going on here so lets try to take it one step at a time.

The variables p and a are only storing the results of calling square's methods. They get whatever value you give them and that value will not change unless you reassign them in your setLength function.

Speaking of, your setLength function is not a method of square. Take a look at how calcArea and calcPerimeter were declared and see if you can figure out how to change setLength to do what you want it to.


#3

Thanks for the reply chrinkus! After reading what you've written I've done a few changes...

I got the variables p and a to return their respective methods. I also changed the setLength variable to what I think is a proper method?

Everything seems to work now! The square.setLength method has an exclamation mark on its return line, but the console.log returns Setting square.sideLength to: undefined if I don't add return. But that's a smaller issue.

Thanks again for taking the time to check this slightly unrelated question out!

Here's the new code:

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;  
};

var p = function() {
  return square.calcPerimeter();
};
var a = function() {
  return square.calcArea();
};

console.log("Perimeter is: " + p());
console.log("Area is: " + a());

// -----The extra code I added is below-----

square.setLength = function(newLength) {
    return this.sideLength = newLength;
};

console.log("Setting square.sideLength to: " + square.setLength(15));
console.log("Current square.sideLength is: " + square.sideLength);
console.log("Perimeter is: " + p());
console.log("Area is: " + a());

The console now logs:

Perimeter is: 24
Area is: 36
Setting square.sideLength to: 15
Current square.sideLength is: 15
Perimeter is: 60
Area is: 225


#4

Good job. Lets get setLength to behave the way you want it to.

A 'return' statement gives whatever value follows the 'return' keyword back to wherever that function was called from. If you 'return' an assignment as you did, there is no value produced, you just set 'sideLength' to the 'newValue' argument and all that the function returns is 'undefined'.

Try moving your assignment of 'sideLength' to its own line above the return statement and instead return the value (or a reference to a value) that you want.


#5

Ohhhh that makes a lot of sense! So then I'd write it like this:

square.setLength = function(newLength) {
    this.sideLength = newLength;
    return this.sideLength;
};

That way the function will assign sideLength an updated value, and the second line will just return the new value so I can see it in my console.log.

My brain is a bit dead from continuing on with the lessons, but I hope I'm getting what you're saying.

Thanks again!


#6

Awesome. Playing around with the code is a great way to gain deeper understanding. Have a good night.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.