Reputation: 33
I am a beginner and new in java script. I have a problem and have tried some ways but problem is here. I wrote a function with 4 parameters and I want to get 4 arguments from input and save them to an array. It means every time gets 4 arguments and save them. this is my code but it is not working.
var personalInfo = {};
function addEmployee(firstName, lastName, hour, salary){
personalInfo = personalInfo.push({'firstName':firstName, 'lastName':lastName, 'hour':hour, 'salary':salary});
return personalInfo;
}
addEmployee(personalInfo);
any solution would be my appreciate.
Upvotes: 0
Views: 330
Reputation: 597
You need to change personalInfo to an array if you want to to push employees to an array. As also mentioned in the comments, you are only passing one parameter but you need to pass four parameters as shown in my example.
You also don't need to assign personalInfo when using push as this pushes a new object to the existing array.
var personalInfo = [];
function addEmployee(firstName, lastName, hour, salary){
personalInfo.push({'firstName':firstName, 'lastName':lastName, 'hour':hour, 'salary':salary});
return personalInfo;
}
addEmployee('first Name', 'last Name', '11', '100');
addEmployee('first Name 2', 'last Name 2', '10', '40');
console.log(personalInfo);
Upvotes: 2