JS Assignment 14: The Class 'prototype'
h
Paultheriault (3)

function exerciseOne(UserClass){
// Exercise One: In this exercise you are given a class called UserClass.
// You will be adding a method to the prototype called greeting
// This method will return the string: 'Hello, it is nice to meet you!'
// DO NOT create a new class or object

// Please write your code in the lines above
return UserClass
}

function exerciseTwo(AnimalClass){
// Exercise Two: In this exercise you are given a class called AnimalClass.
// The class will already have the properties 'name', 'noise' on it.
// You will be adding a method to the prototype called 'speak'
// Using the 'this' keyword, speak should return the following string:
// '<name> says <noise>'
// DO NOT create a new class or object
AnimalClass.prototype.speak = function()
{
this.speak = name + 'says'; noise;
return this.speak ;
};

// Please write your code in the lines above
return AnimalClass;
}
I am having a hellacious time with these two, can I please get some assistance?

You are viewing a single comment. View All
Answered by albertinospecie (6) [earned 5 cycles]
View Answer
vedprad1 (744)

Here are the answers. For more resources, check w3schools.com.

function exerciseOne(UserClass) {
  UserClass.prototype.greeting = function () {
    return 'Hello, it is nice to meet you!';
  };
}

function exerciseTwo(AnimalClass) {
  AnimalClass.prototype.speak = function () {
    return this.name + ' says ' + this.noise;
  };
}

If this answers your question, please check the checkmark on the left side of this message. Thanks and Good Luck!

albertinospecie (6)

@vedprad1 This is what I did below:

function exerciseOne(UserClass){
// Exercise One: In this exercise you are given a class called UserClass.
// You will be adding a method to the prototype called greeting
// This method will return the string: 'Hello, it is nice to meet you!'
// DO NOT create a new class or object

UserClass.prototype.greeting = function () {
return 'Hello, it is nice to meet you!';
};
}

albertinospecie (6)

@vedprad1 I still get the error on my screen "Should add the greeting method to the prototype of UserClass
TypeError: NewUser is not a constructor". What could i be doing wrong?

vedprad1 (744)

@albertinospecie : Well, you never said NewUser anywhere in this function, so it must be somewhere else.

Also, put your code inside code blocks. It makes it easier to read.

Paultheriault (3)

@vedprad1 Sorry I am such a noob. I am supposed to be starting an online coding school in just a couple of weeks and for some reason I am floundering in Javascript. I have gone through various tutorials, bootcamps, and other online resources and I am able to fill in the blanks on those, enough that I have earned "certifications" in Javascript on several sites, but when it comes to applying the principals to my own problems I fall flat on my face. The school is a great opportunity and I would hate to mess it up but I don't know what I am doing wrong. I know one thing, when it comes to functions, methods, and prototypes I am lost and it is stressing me out to the max.

helynoftroy (1)

@vedprad1 i entered what you did for the second exercise and i get the same error as well.

AnimalClass.prototype.speak = function () {
return this.name + says + this.noise;
};
}
// Please write your code in the lines above
return AnimalClass
}

not sure what i am doing wrong. i got the first exercise rather quickly but for some reason its not even taking your code you wrote above. i have even tried the ${this.name}

Voden993 (0)

@Paultheriault man I feel ya, I'm glad you asked this!

vedprad1 (744)

@helynoftroy : This is not my question. It's @Paultheriault 's. I am not in your class.

Anyways, there is an extra closing curly brace (}) after the prototype function, and the semicolon at the end of the return statement is missing.