Ask coding questions

← Back to all posts
JS Assignment 13 Intro to Classes in Javascript
nda1 (5)

Hey could anyone who has done the lambda precourse work help me, I do not understand why my answer is wrong. here is the assignment link: https://repl.it/student/submissions/6438375

function ClassOne(name, pw, mail){
// Exercise One: In this exercise you will be creating your own class!
// You are currently in the class, you are given three strings, name, pw, and mail.
// You need to create three properties on this class.
// Those properties are: 'username', 'password', and 'email'
// Set the value of username to name,
// Set the value of password to pw,
// Set the value of email to mail

this was my answer>>>
function ClassOne(username, password, email) {
this.username = name;
this.password = pw;
this.email = mail;
}

Commentshotnewtop
GiovaniPortal (1)

Might be late of an answer but to solve this you must remember that the function already given to you here: function ClassOne(name, pw, mail){
}

So your answer is correct:

this.username = name;
this.password = pw;
this.email = mail;

just don't write:

function ClassOne(name, pw, mail){
}

because it is already written for you...

JSer (1144)

Try this:

function ClassOne (name, pw, mail) {
    this.username = name;
    this.password = pw;
    this.email = mail;
}

You were just naming the parameters wrong

Pythonier (303)

Try this:

function ClassOne(username, password, email) {
name = this.username;
pw = this.password
mail = this.email
}

or this:

function ClassOne(name, pw, mail) {
username = this.name;
password = this.pw;
email = this.mail;
}
nda1 (5)

@Pythonier this didn't work, i still got an error

nda1 (5)

@Pythonier and I am still getting an error of undefined. the message stating "Expected undefined to be 'Dan'."

ryanhcode (91)

You are creating a function, when the question is asking you to create a class. Hope this helps point you in the right direction, and if you need more details, let me know!

a5rocks (533)

if everyone else's answers don't work, check out constructors.

ArchieMaclean (646)

@a5rocks Constructors are for classes I think, like

class A {
  constructor(a,b,c) {
    // blah
  }
}

Not JS's weird function-class things.

Maybe it works with them as well, but it's not needed here I don't think :)

nda1 (5)

@ArchieMaclean I figured it out the program did not want the extra
function ClassOne(name, pw, mail).

it accepted just >>
this.username = name;
this.password = pw;
this.email = mail;
thank you so much for all the help you gave me, you truly were a lifesaver. :)

ArchieMaclean (646)

@nda1 Good, I'm glad it worked out :)

ArchieMaclean (646)

When you declare a variable, the left side has to be not known, and the right side has to be known.
When you are doing

this.username = name;

the computer does not know the right hand side of the equation - you never said what name was.
I'm not giving you the whole answer, but hopefully you should be able to work it out from that :)

Please upvote if this helps :)

nda1 (5)

@ArchieMaclean I’m sorry, I’m still confused.

ArchieMaclean (646)

@nda1 This is your answer:

function ClassOne(username, password, email) {
  this.username = name;
  this.password = pw;
  this.email = mail;
}

But you never say what name, pw or mail are, so the computer gives an error when you say

this.username = name

Because it doesn't know what name is. This happens when declaring all 3 variables.

nda1 (5)

@ArchieMaclean do you mean like this?

const ann = new ClassOne ('Ann', '1qwert', '[email protected]');

ArchieMaclean (646)

@nda1 No. If you look at the code you made:

function ClassOne(username, password, email) {
  this.username = name;
  this.password = pw;
  this.email = mail;
}

you are never saying what name is equal to. At the top, you have

function ClassOne(username, password, email) {

, but you never declare name, pw or mail.

nda1 (5)

@ArchieMaclean but it is just asking to assign those values, not declare it.
}

nda1 (5)

@ArchieMaclean the assignment did not ask me to the declare it so I assumed i did not have too.

ArchieMaclean (646)

@nda1 No not like that. I'm just going to give you the solution.
Your code is like this:

function ClassOne(username, password, email) {
  this.username = name;
  this.password = pw;
  this.email = mail;
}

However, I have explained above that this doesn't work. To fix it, you need to do this:

function ClassOne(name,pw,mail) {     // this has been changed
  this.username = name;
  this.password = pw;
  this.email = mail;
}

Now you've done that, the computer knows what name, pw and mail are, so it runs.

nda1 (5)

@ArchieMaclean I tried that before and it did not work.

ArchieMaclean (646)

@nda1 Hmm.. it should do. Do you have a link to the repl?

ArchieMaclean (646)

@nda1 I don't have permission to that. Could you copy-paste it into a repl?

nda1 (5)

@ArchieMaclean i tried posting it in the javascript one but it keeps defaulting to node.js but here it is https://repl.it/@nda1/practice-problem

ArchieMaclean (646)

@nda1 Sorry for giving you the wrong repl type ;(
Here is a fixed version of the repl.

nda1 (5)

@ArchieMaclean sorry but I am still getting an error. the message stating "Expected undefined to be 'Dan'."

ArchieMaclean (646)

@nda1 Do you get this when you run my repl? If not, what did you change?

nda1 (5)

@ArchieMaclean yes, I got that message when i ran your code but also when I ran mine.

ArchieMaclean (646)

@nda1 I don't get any error when running my code...

Zavexeon (444)

You have to declare the variables like this: var var_name = value;

Because this is an assignment, that's as much as I'll tell you.

Good luck. :)

ArchieMaclean (646)

@Zavexeon That's not the reason that his code is wrong though - you don't have to do that for attributes.