Home » Javascript » arrays – Javascript insert prompt value to another function object

arrays – Javascript insert prompt value to another function object

Posted by: admin February 26, 2020 Leave a comment

Questions:

I’m trying to put the values at addUser function to push to another function object. I dont how and what to put at the prompt value to .id and .pwd.

var memArray  =[];

function addUserObject(id, password){
 this.id          = id; 
 this.pwd         = password
 }
 var addUserObj = new addUserObject ("") 
 // i dont how and what to put the prompt value to .id and .pwd

 memArray.push(addUserObj);
 console.log(memArray)


  function addUser(){
  var addUsername = prompt ("Type your username");
  var addPwd = prompt ("Type your password");
  addUserObject(addUsername,addPwd)
How to&Answers:

Once you create an object, it has to contain properties along with methods, so it’s ok to implement all inside an instance.

var memArray  =[];

function addUserObject(){
 var id, pwd;
 this.getData = function(){
    return {
    id: this.id,
    pwd: this.pwd
    }
 }
 this.setData = function(){
    this.id = prompt ("Type your username");
    this.pwd = prompt ("Type your password");
  }
  return this.setData();
}

var user = new addUserObject;
 
 memArray.push(user.getData());
 console.log(memArray)

Answer:

At the risk of not having understood your problem, you can’t add the user until you know the username and password (until the prompt requests are finished).

Is this what you are trying to do?:

// Initialize an empty array
var memArray  =[];

// Define helper function to add a user to the array
function addUserObject(id, password){

	// Define a user object
	var userObj = {
		id: id,
		password: password
	};

	// Push the new user into the array
	memArray.push(userObj);
}

// Define a function that requests user and pwd
function addUser(){

	// Request username and pwd
	var addUsername = prompt ("Type your username");
	var addPwd      = prompt ("Type your password");

	// Call to add the user to the array
	addUserObject(addUsername, addPwd);
}

// Call the 'addUser' function to request a new user
addUser();

// Print the array to console (it should contain one user)
console.log(memArray);

I have commented the code excessively just so you understand.

Answer:

One way is to add the functionality to this, same as the properties you create:

function addUserObject(id, password){
  this.id = id; 
  this.pwd = password;
  
  this.addUsername = function(){this.id = prompt("Type your username")}.bind(this);
  this.addPwd = function(){this.pwd = prompt("Type your password")}.bind(this);
};

var addUserObj = new addUserObject();
 
//REM: Adding username
addUserObj.addUsername();
console.log(addUserObj);
 
 //REM: Adding paddword
addUserObj.addPwd();
console.log(addUserObj);

The second way is to ask for it on creation:

function addUserObject(id, password){
  this.id = id; 
  this.pwd = password;
}

var addUserObj = new addUserObject(prompt("name?"), prompt("pw?"));
console.log(addUserObj);

Answer:

If you want to use constructors functions, then you can create a function which return a new object:

function userObject(id, name, password){
    this.id = id;
    this.name = name;
    this.pwd = password
}

let userBob = new userObject(1, 'Bob', 'fooPsw');
let userJoseph = new userObject(2, 'Joseph', 'barPsw');
let userJohn = new userObject(3, 'John', 'barPsw');

and then just push these objects into array:

let users = [];
function addUser(id, name, password)
{    
    let user = new userObject(id, name, password);
    users.push(user);
}

addUser(1, 'Bob', 'fooPsw');
addUser(2, 'Joseph', 'barPsw');
addUser(3, 'John', 'barPsw');

The whole code looks like this:

function userObject(id, name, password){
    this.id = id;
    this.name = name;
    this.pwd = password
}


let users = [];
function addUser(id, name, password)
{    
    let user = new userObject(id, name, password);
    users.push(user);
}

addUser(1, 'Bob', 'fooPsw');
addUser(2, 'Joseph', 'barPsw');
addUser(3, 'John', 'barPsw');


console.log(users);