0

I'm trying to create two-column array and insert few rows. I noticed that third row is not getting through due to error in my code. Currently it seems to has wrongly defined structure where one pair is assigned to first row, second pair goes to second column, third is skipped.

And my goals is to have NAME|Salary table.

//2D array
var e = [];
var numberofemployees = 0;

function addEmployees(name, salary){
    var arMod = e.push([name,salary]);
    return e;
    //return  Object.keys(arMod).length;
}

addEmployees(["Mark", 5000], ["Jack", 1500], ["Maria", 2000]);

2 Answers 2

1

Your function is designed to take two arguments, not three pairs. So call it like:

addEmployees("Mark", 5000);
addEmployees("Jack", 1500);
addEmployees("Maria", 2000);

If on the other hand you want to be able to call the function like you did, then you need to change the function so it can deal with that format:

function addEmployees(...pairs){
    return e.concat(pairs);
}
addEmployees(["Mark", 5000], ["Jack", 1500], ["Maria", 2000]);

The code you have in comments seems to indicate you expect that arMod is some object, but it is a number: push returns the length of the array.

Sign up to request clarification or add additional context in comments.

Comments

0

Your method only accepts two arguments, so you are adding those first two arrays you are passing to your method to the array.

You would need to call the addEmployees() Method for each array(name/salary pair) individually

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.