Using prompt as object selector in javascript


#1

Hi, can I use the input from a prompt box as an object selector? Something like:

function Person(firstname, lastname, age, username, password){
this.firstname = firstname,
this.lastname = lastname,
this.age = age,
this.username = username,
this.password = password
};
var pete = new Person("Pete", "Gogh", 6, "pete", "1234");

var a = prompt("What is your username?");
console.log(a.age);


#2

You can just the way you're doing it. Assign prompts to variables, then run the variables through object constructor, notify user that object has been created.


#3

Hey, @cadecodes, I also have an if/else statement worked into it:

function Person(firstname, lastname, age, username, password){
this.firstname = firstname,
this.lastname = lastname,
this.age = age,
this.username = username,
this.password = password
};

var pete = new Person("Pete", "Gogh", 6, "pete", "1234");

var userName = prompt("Enter Your Username:");
var passWord = prompt("Enter the password for " + userName);
if(passWord = userName.password) {
confirm("Welcome back, " + userName);
}
else {
confirm("sorry, wrong password. Your password is: " + userName.password);
};

When I run this and put in the correct info, it always confirms: "sorry, wrong password. Your password is: undefined". I'm not sure why it doesn't work....any help would be greatly appreciated.


#4

Just a tip, keep your capitalization the same, it looks very messy. The problem is the use of userName.password. Do you know why?


#5

userName.password is to check if the password entered is the one pertaining to the username that was entered. Can I use the variable userName (The prompt asking the user to enter his username) as an object selector? pete.password returns the password for pete, so if the userName prompt is set equal to 'pete', why doesn't userName.password work?


#6

It isn't set equal to pete as far as I can tell, you could assign each new person to something like userName1, userName2, etc.