Ask coding questions

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

function ClassTwo(name, pw, mail){
// Exercise Two: Now that you have created your own class,
// you will create a class with a method on it.
// In this class create 4 properties: username, password, email, and checkPassword.
// Set the value of username to name,
this.username = name;
// Set the value of password to pw,
this.passwod = pw;
// Set the value of email to mail
this.email = mail;
// Set the value of checkPassword to a function.
this.checkPassword = function(){

}
// The checkPassword function takes a string as it's only argument.
// Using the 'this' keyword check to see if the password on the class is the same as the string being passed in as the parameter. Return true or false.

}

What am I missing in the function?

Answered by a5rocks (535) [earned 5 cycles]
View Answer
Commentshotnewtop
a5rocks (535)

Your checkPassword function needs to accept a string as an input, and compare it to the password. It would look something like so:

checkPassword = function(stringInput){return stringInput == this.password}

Any questions about the code?

maximussallam (5)

@a5rocks aaahhhhh!!!! I was really close on this one. I see where I was going wrong now. All my attempts at completing this function were just slightly off. Thank you so much for your assistance. I really appreciate you!

maximussallam (5)

@a5rocks Unfortunately, I'm getting this error: Expected undefined to be 'abc123'.

I have this in the repl:

function ClassTwo(name, pw, mail){
// Exercise Two: Now that you have created your own class,
// you will create a class with a method on it.
// In this class create 4 properties: username, password, email, and checkPassword.
// Set the value of username to name,
// Set the value of password to pw,
// Set the value of email to mail
// Set the value of checkPassword to a function.
// The checkPassword function takes a string as it's only argument.
// Using the 'this' keyword check to see if the password on the class is the same as the string being passed in as the parameter. Return true or false.
this.username = name;
this.passwod = pw;
this.email = mail;
checkPassword = function(string){
return string === this.password;
};
}

vedprad1 (817)

@maximussallam : You spelled password wrong.
If this answers your question, please check the checkmark on the left side of this message. Thanks and Good Luck!

maximussallam (5)

@vedprad1 after correcting the misspelling, I'm getting this error: Expected 'undefined' to be 'function'.

a5rocks (535)

@maximussallam oh sorry I was offline... this.checkPassword instead of checkPassword... Sorry my mistake.

WonReasley (2)

I just completed this exercise and just want to point out that the solutions here all leave out that you need an if/then statement to make it work, or at least I did:

this.username = name;
this.password = pw;
this.email = mail;
this.checkPassword = function(pw){
if (this.password === pw) {
return true;
}
return false;
};
}

Also, where it says function(pw), it's important to note that "pw" can literally be anything. I even typed in "poop" and it worked as well, it just needs to be a string (any string) set to equal to this.password in the conditional statement.

I imagine there is some function in javascript that allows user input and then you would do something like function(userInput()) { if (this.password === userInput()){return true} or something to verify actual passwords, but I haven't gotten that far yet.