0

OK, so I am attempting to write some code that will take x number of classrooms and x number of students per classroom and then pair two students together randomly from separate classrooms.

Sounds Simple, right! I have already been able to create x number of input elements dynamically on the page and all works up until I try to create the array for the x number of elements.

Is there a different approach I should Try?

I got my code working up til this point:

 for (i = 0; i < classSize; i++) {
     var arrField = container2.appendChild(document.createElement('fieldset'));
     var arrDiv = arrField.appendChild(document.createElement('div'));
     arrDiv.id = "div" + (i + 1);
     var studentArr = "studentArr" + (i + 1);
     alert(studentArr); //returns "studentArr1" "studentArr2", etc.

     for (n = 0; n < arrSize; n++) {
         studentArr = new student();
         studentArr[(n + 1)] = new student(document.getElementById("name" + (n + 1)).value, document.getElementById("email" + (n + 1)).value, document.getElementById("phone" + (n + 1)).value);
         alert(studentArr[(n + 1)].name);
         alert(studentArr[(n + 1)].email);
         alert(studentArr[(n + 1)].phone);
     }
 }

FYI- I am very new to JavaScript and programming in general!- Sorry

Roy M J
  • 6,926
  • 7
  • 51
  • 78
Chris
  • 185
  • 1
  • 8
  • Can you add an example on jsfiddle.net with the whole code in question? Also, an easier way of adding elements to an array...instead of `studentArr[(n+1)] = ...` use `studentArr.push(...)` – Villarrealized Nov 16 '13 at 18:00
  • Could you elaborate on "... until I try to create the array for the x number of elements"? What do you mean by "elements"? – DashK Nov 16 '13 at 18:08
  • Why are you giving them sequential IDs if you have a reference to them in an array? – Benjamin Gruenbaum Nov 16 '13 at 19:45
  • @DashK I am referring to the x number of input elements in html that were created dynamically. – Chris Nov 16 '13 at 22:12
  • @Villarrealized I will try the using .push an see if that works. – Chris Nov 16 '13 at 22:14
  • @BenjaminGruenbaum I am under the impression that i need to refer to the id that was created dynamically for the input elements to be inserted into the array? – Chris Nov 16 '13 at 22:17
  • @Chris, it won't fix your problem, your way works fine it's just a little more concise and readable. – Villarrealized Nov 16 '13 at 22:50

2 Answers2

0

Use :

array.push();

Example :

// initialize array
var arr = [
    "Hi",
    "Hello",
    "Bonjour"
];

// append new value to the array
arr.push("Hola");

// display all values
for (var i = 0; i < arr.length; i++) {
    alert(arr[i]);
}
Roy M J
  • 6,926
  • 7
  • 51
  • 78
0

Have you defined the student object constructor elsewhere? As it stands, student() looks like it's standing in for both an array and an object with attributes like name, email, etc. You should use something along the lines of

studentArray = []
studentArray.push(new student(name,email,etc.))

in order to prevent overloading the student class.

Also, on a separate note, you shouldn't use alerts unless you literally would say, "Alert!" They are problematic for at least three reasons: 1) They're really annoying 2) alert(object) will display something like [Object Object] for anything more complicated than a simple data structure like a string 3) They prevent anything else from running on the page.

You're better off using console.log, which writes information to the JavaScript Console. Different browsers implement the console in different places, but in Chrome, the console is in View -> Developer -> JavaScript Console. console.log is less obtrusive and allows you to inspect objects much more thoroughly than alert does.

elreimundo
  • 6,186
  • 1
  • 13
  • 6
  • I am marking your answer as the solution as it pointed out the problem i was also having with [object,Object], i am now using the console.log more religiously now....thanks! here is a link to the JSFiddle that works so far.....http://jsfiddle.net/I_am_Chris/Yn5Wy/ – Chris Nov 19 '13 at 21:00
  • I am still trying to figure out how to pull a random student from each classroom and pair them with another student from a different classroom???? – Chris Nov 19 '13 at 21:05