Assessment wanted for OOJS 1/2/3 skill test!

I think my solution works, just bit of messy, anyone has any suggestions about how to make the code more readable, and any tips for good coding habits?

Hi there @Ye_Cai, and welcome to our community.

You are definitely on the right track with your answers here, in terms of style and content. In general, your code spacing looks OK. You do need to think about putting semi-colons on the end of each instruction. This can lead to unexpected results if you’re not careful.

For the first assessment, I ended up with this:

// Write your code below here
Shape.prototype.calcPerimeter = function() {
  console.log(this.sides * this.sideLength);
}


var square = new Shape('square', 4, 5);
square.calcPerimeter();

var triangle = new Shape('triangle', 3, 3);
triangle.calcPerimeter();

You basically had all this; the only real difference is that I’ve got the calcPerimeter() to log the result to the console rather than returning it, which is what the question asks for. Which the logic is fine.

Assessment 2 looks fine, with the save caveats pointed out above.

For assessment 3, you were nearly there. My version worked out like this:

class Shape {
    constructor(name, sides, sideLength) {
        this.name = name;
        this.sides = sides;
        this.sideLength = sideLength;
    };

    calcPerimeter() {
        console.log(this.sides * this.sideLength);
    }
}



    class Square extends Shape {
    constructor(sideLength) {
        super('square', 4, sideLength)
    }
    calcArea() {
        console.log(this.sideLength * this.sideLength);
    }

}

var square = new Square(5)
square.calcArea();
square.calcPerimeter();

Because name and sides will always be “square” and “4”, you specify those actual values when calling super(), so they will always be available in new instances.

Then you only need to pass sideLength into the constructor, as that’s the only one you need to specify.

2 Likes

Thanks bro! That console definitely a mistake!I was running it with Node, grt tips!

Where does the “.prototype” come from?

1 Like

@timandes the prototype object is basically where you define parts of an object class’s functionality that will be inherited by other classes.

Modern JS actually has class syntax available, which is a much easier way of writing classes that inherit from other classes (the same thing happens under the hood, but the syntax is much less confusing). you can read about this at https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance#ECMAScript_2015_Classes, and in fact it might make sense for you to start with this bit and then read the rest of that article.

this is one of the most complex bits of JavaScript, so don’t be worried if you find it confusing to begin with. You’ll get there in time. You won’t even use all this stuff all that often, but it’s useful to understand as you start writing more complex JavaScript.

2 Likes

Aahh, ok thank you!

“Note that this set of tests relies on some of the knowledge taught in the next two articles, so you might want to read them first before you try the tests.”

I missed that part at the end of Object Oriented JS. I’ll get back to that assessment after the next to sections and re-try it.

Thanks, as always, Chris!

1 Like

@timandes you are welcome, good sir!

1 Like

Hi @chrisdavidmills

I did this something like this. It works but it is different from what you have done. Can you please tell me if it is okay and if it is not okay, then why?

Here is my code

https://codepen.io/harshitbadolla/pen/wvMVGJJ

@Harshit_Badolla this approach is totally reaonable — there is always more than one way to solve a problem!