Object math exercise

Hello all.

Exercise: Define an object called “square” which will hold methods that have to do with the geometry of squares. It should
contain two methods, area and perimeter.
Rules

  1. Area should accept the length of a side and then return the side squared.
  2. Perimeter should accept the length of a side and return that side multiplied by 4.

Here is what I have so far.

const square = new square() { square.area(side), square.perimeter(side); }

I think I’m missing a lot but have the basics. I think my parameters are wrong. Appreciate the help.

Is that all of your code? It seems like a lot is missing.

That is all my code so far yes. Unfortunately, objects are pretty new to me and I’m having trouble w/ the proper appending and parameters needed.

There appears to be no definition of the object or a constructor.

Something like this:

Honestly, this course does not seem to be working for you at all. You have basic syntax errors and seem to be missing key pieces of understanding. I would recommend you consider trying another course.

1 Like

Fixed and learned:

const square = { area (side) { return side * side; }, perimeter (side) { return side * 4; } };
I don’t know how to make it look properly formatted for this editor but that’s the correct code to complete the exercise.

That’s a somewhat difficult to read way to write that

const square = {
    area(side) {
      return side * side;
    },
    perimeter(side) {
      return side * 4;
    },
};

I don’t see any way for this object definition to know what it’s side length is?


Edit: Oh, rereading… this is just an object that only holds two methods… I have no idea why your course is asking you to do what seems to me to be a pointless exercise.

Okay, lol. I’ve about had it w/ your judgment on what I’m asking the community’s help with dude. I’m an intro programmer and its a Udemy web dev bootcamp. If you aren’t going to just help and instead add pointless criticism of where I’m learning from, then don’t bother posting on my code.

//Edit (just for Jeremy): It's for learning. That's why the instructor of the course is asking me to do the exercise.

I did not mean to upset you. But you should know that this not really something that comes up as a realistic use case. Just because someone is selling a course doesn’t mean everything in it is good.

“It’s for learning” but with unorthodox stuff that isn’t really used is a dubious reason to teach something in my experience. It might be a stepping stone, but it is a strange stepping stone if so.

Fair enough but I have to learn somehow and its a free course from someone else who learned the same way. To me, what you’re referring to is a higher level area of coding that I’m just not ready for.

Edit: Its a small stepping stone is what I think you mean. And I’m fine w/ that. Baby steps.

Its not so much a ‘higher level’ of coding so much as questioning if what you are being taught has a lot of use? Teaching objects is useful. Teaching objects with methods but zero internal data is strange. It could be that the person who made this course has a good reason or a great plan, but some of what you you have shown don’t really make sense as stepping stones to me.

People learn this as a career in different ways. I’ve seen self learned, I’ve seen bachelors in CS, I’ve seen bootcamps. I might join one of those after this course. Who knows? But I didn’t know (don’t know lol) the basics to at least get $22-$25 an hour paid internship locally to clean code up. So, important to know.

Edit: And I’d like to develop mobile apps and eventually mobile games. So this is an important step. I can’t just jump in C# in Unity lol.

I in no way object to self learning, boot camp learning, or university learning. All are perfectly great ways to learn how to code. I’m just questioning the pieces of content you have shared from one specific course.

Seeing code in isolation also makes it harder for us to see the bigger picture. This exercise might just be part of a larger instruction set that goes more into detail about objects and methods. As such it might not be all that odd of an exercise.

Sure, it is definitely hard to judge from a handful of random pieces that were confusing to one person.

So all fair. But the exercises I’ve been posting have been/are wrap up exercises after a section of something learned from the instructor. Methods, loops etc. Then we’re tested on something he creates. I like that way because I have to keep doing it until I build something and pass. They’re daily, small enough to actually learn and actually usable/real code.

I just meant from our perspective it is harder to gauge the overall point of an exercise in isolation as it might be part of a larger set of exercises. I’m not suggesting you are not learning or that the course doesn’t have value. I’m only trying to explain why we might find some exercises odd when only seen in isolation without the bigger picture.

Anyway, just keep at it, happy coding.