Ask coding questions

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

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
2
Pythonier (299)

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;
}
1
nda1 (1)

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

2
1
nda1 (1)

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

1
ryanhcode (85)

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!

1
JSer (1088)

Try this:

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

You were just naming the parameters wrong

1
a5rocks (501)

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

1
ArchieMaclean (523)

@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 :)

1
nda1 (1)

@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. :)

1
ArchieMaclean (523)

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

1
ArchieMaclean (523)

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 :)

1
nda1 (1)

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

1
ArchieMaclean (523)

@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.

1
nda1 (1)

@ArchieMaclean do you mean like this?

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

1
ArchieMaclean (523)

@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.

1
nda1 (1)

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

1
nda1 (1)

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

1
ArchieMaclean (523)

@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.

1
nda1 (1)

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

1
ArchieMaclean (523)

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

1
ArchieMaclean (523)

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

1
nda1 (1)

@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

1
ArchieMaclean (523)

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

1
nda1 (1)

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

1
ArchieMaclean (523)

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

1
nda1 (1)

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

1
ArchieMaclean (523)

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

1
Zavexeon (202)

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. :)

1
ArchieMaclean (523)

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