Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

java script function via 4 parameters

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading