What is wrong with this code, Your method failed when sideLength was 0 where it returned NaN instead of 0


#1

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


#2

Your code looks fine, but I am getting the error too when I use your code.

However when I indent it like so.

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

It works for me. But your code is right and should be working too. Try copy and pasting mine and let me know if that worked.


#3

What is wrong with this code, Your method failed when sideLength was 0 where it returned NaN instead of 0

You need to see these snaps that I observed and I will also give you reason why your code throwing Error.

Lets see what question is asking?

Now lets observe your code!

You mistakenly wrote
sideLength as sidelength !
If it solves your problem ,let me know!
If not , I'll glad to help! do ask! :slight_smile:

Update**
Mystery of NaN type--
Read about NaN
Another resource


#4

@greentreemee very impressive I stared at this code for a while and didn't pick that up. I then re wrote it and it passed. Not realising that i already changed the sidelength to sideLength haha.


#5

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