Ask coding questions

← Back to all posts
Need help with assignment 12: Lambda
FINALBOSS (5)

In assignment 12 of the precourse, I'm asked to demonstrate the proper use of methods in the javascript language. The lesson in accordance to this assignment also covers loops and .this. I'm not sure if I have to demonstrate those for exercise one, but we'll see.

Anyways, I'm already given an object called mathHelpers. I have to create a method within the object and call it 'double'. The double method must take one parameter, a number and the number must be returned with a multiple of 2.

Problem: I don't know if I'm overthinking it or what, but everything I try doesn't seem to work. I keep getting the unidentified error after declaring the method as a function and writing the code within.

code

function exerciseOne(){

  let mathHelpers = {
 
double : function (numX) {

return mathHelpers.double(5) * 2;

                                }
};
  }
Commentshotnewtop
d_d (1)

There's no need to reference the object or method name within your double function:

double: function (numX) {
  return numX * 2
}

If you wanted to use double, you could do so like this: console.log(mathHelpers.double(5))

So it looks like you mixed up defining a method and calling that method.

FINALBOSS (5)

Hey man, thanks for your help, but it didn't work. I knew it wouldn't work because that's what I did the first attempt at the assignment. @d_d

FINALBOSS (5)

I found help on the slack website. Apparently, all I had to do was add "return mathHelpers". Sheesh, coding can be so prudent sometimes.

FINALBOSS (5)

Here is the multiplayer room. I really need help with this assignment. https://repl.it/join/twjtevao-finalboss

FINALBOSS (5)

The deadline for all assignments is drawing near. Anyone willing to come into a multiplayer room and help walk me through this part of the assignment. It's starting to irritate me, lol.

FINALBOSS (5)

It doesn't seem hard, it's just that the instructors aren't very specific about how they want it to be [email protected]

mwilki7 (533)

Did you try:

function exerciseOne()
{
    ... // other code here
    mathHelpers.double = function(num)
    {
        return num * 2;
    };
}
FINALBOSS (5)

It gave me errors when I tried that. @mwilki7

mwilki7 (533)

@FINALBOSS
What do they errors say?

FINALBOSS (5)

It doesn't give me errors when I take the function you gave me out of the mathHelpers object and fix up a few loose semicolons and brackets.
However, the point of the assignment is to write code within mathHelpers object. @mwilki7

Geocube101 (358)

You'll need to first create a class called mathHelpers

class mathHelpers
{
  //Class code
}

You can then create a method inside the curly brackets...

class mathHelpers
{
  double(x)
  {
    return x * 2 //I think you said that the number must be returned multiplied by 2?
  }
}
FINALBOSS (5)

@Geocube101 thanks for the reply, but we haven't covered Classes yet. That'll be in the next lesson. We're just focusing on Objects, Methods, Loops and .this for now.

Geocube101 (358)

@FINALBOSS Ok?
well, then can you use .prototype to add a method to a pre-existing class?

FINALBOSS (5)

@Geocube101 No, because classes isn't supposed to be used in this assignment yet. .prototype hasn't been covered either. mathHelpers is already given to me as an object, so turning it into a class would be against the rules.

Geocube101 (358)

@FINALBOSS I have no idea how to do this, but I found this article that may help

FINALBOSS (5)

You know what? I actually go to this site quite often to learn W3.CSS markups for my website. Glade to know other ppl know of it. Thanks for the reference, I'll take a look. @Geocube101