0
FirstName = FirstName.value;
LastName = LastName.value;
Adress = Adress.value;
for(i=0 ;i<10;i++){
    contacts[i] = [];
    for(c=0;c<3;c++){
        contacts[i].push(FirstName);
        contacts[i].push(LastName);
        contacts[i].push(Adress);
    }
}

the code gives me the contacts array with 10 arrays and each array has the information repeated 3times

2 Answers2

0

You don't need the second for loop:

FirstName = FirstName.value;
LastName = LastName.value;
Adress = Adress.value;
for(i = 0; i < 10; i++) {
    contacts[i] = [];
    contacts[i].push(FirstName);
    contacts[i].push(LastName);
    contacts[i].push(Adress);
}

Or you can also use this way:

FirstName = FirstName.value;
LastName = LastName.value;
Adress = Adress.value;
for(i = 0; i < 10; i++) {
    contacts[i] = [FirstName, LastName, Adress];
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

I am not sure on the objective but I think you should be using objects to store your information instead of 2D arrays. (Jsfiddle that show 2d array and array with objects). So instead of

var contacts = [
  ['John', 'Doe', '100 Main Street'],
  ['Jane','Smith','101 Main']
];

you would have

var contacts = [
  {
    first_name: 'John',
    last_name: 'Doe', 
    address: '100 Main Street'
  },
  {
    first_name: 'Jane',
    last_name: 'Smith', 
    address: '101 Main Street'
  }
];

Try this. It will push the form name and email onto the contacts

<form onsubmit='formSubmit(event);'>
  <label>name</label>
  <input id='name' value='John'/>
  <label>email</label>
  <input id='email' value='fake@test.com'/>
  <br/>
  <button>
  add name and email
  </button>
</form>

function formSubmit(event){
    event.preventDefault();
    contacts.push({
      name: document.getElementById('name').value,
      email: document.getElementById('email').value
    })
    console.log(contacts);
}
Steven Kaspar
  • 1,147
  • 10
  • 14
  • but how to make the script create an object each time the user fill the informations –  Apr 10 '16 at 14:07