This works now thanks to the great help of Thunda and ArtyMcFly. For the entire code please look here: http://plnkr.co/edit/Imcl53?p=info Thanks for this suggestion Thunda. First please let me apologize for this sheer amount of code. I just don't know how else to explain my problem in its entirety...
I am new to JS but not programming. I want to do the following:
A user puts his firstname, lastname, age and selects his gender. With a click on the Start button an object is created based on the constructor Mitarbeiter. So far everything is working except that I want to save the values into a 2D-Array.
runningNr works as a counter as well as an ID. Afterwards I want to show the 2D array in a table.
My problem: obviously my saveToArray() and showArray() methods cause my entire JS code to not work anymore. I tested this by commenting out both methods and the respective this.saveToArray = saveToArray() and this.showArray = showArray().
I also am not too sure how to actually implement my showArray() method but if I can't even save the values there is no point in thinking about that yet. :/
In addition I am only allowed to do this task with JS.
var runningNr = 0;
var employee = new Array(); // 2D-Array
// Konstruktor
function Mitarbeiter(f, l, a, g) //f = forename, l = lastname, a = age, g = gender
{
var fname = f;
var lname = setLName(l);
var age = a;
var gender;
if (g == "Mann")
gender = false;
else
gender = true;
this.forename = getFName();
this.lastname = getLName();
this.age = getAge();
this.gender = getGender();
var total = runningNr + 1; // total of objects. runningNr +1 cause runningNr starts at 0
this.total = getTotalObj();
var checkboxes = document.forms.meinFormular.elements.gender;
this.saveToArray = saveToArray();
/* this.showArray = showArray(); */
// Getters coming your way
// Setters coming your way
function setLName(x)
{
lname = x;
}
function saveToArray(runningNr, fname, lname, age, gender)
{
employee[Id] = new Array();
employee[Id]["ID"] = runningNr
employee[Id]["Firstname"] = fname;
employee[Id]["Lastname"] = lname;
employee[Id]["Age"] = age;
employee[Id]["Gender"] = gender;
runningNumber++; // Increment as this is the ID as well as the row of 2D-Array
}
function showArray() // Creating a table to show all informations about employees
{
var myTable = "<table>";
myTable += "<tr>";
myTable += "<th> ID </th>";
myTable += "<th> Vorname </th>";
myTable += "<th> Nachname </th>";
myTable += "<th> Alter </th>";
myTable += "<th> IstFrau </th>";
myTable += "</tr>";
for (var i = 0; i <= runningNumber; i++)
{
for (var j = 0; j <= 5; j++)
{
myTable += "<tr>";
myTable += "<td>"
employee[i][j]
"</td>";
}
myTable += "</tr>";
}
myTable += "</table>";
document.getElementById("showContent").innerHTML = myTable;
}
}
function createObject(fname, lname, a, g)
{
this.g = g[g.selectedIndex].text; // Selected gender (Mann or Frau) is given as a parameter to Mitarbeiter();
var obj = new Mitarbeiter(fname, lname, a, this.g);
obj.saveToArray
}
<input type="text" name="forename" id="forename" value="test">
<input type="text" name="lastname" id="lastname" value="test">
<input type="text" name="age" id="age" value="1">
<select id="gender" name="gender" size="2">
<option selected> Mann </option>
<option> Frau </option>
</select>
<input type="button" value="Start" onclick="createObject(this.form.forename.value, this.form.lastname.value, this.form.age.value, this.form.gender)">
<input type="button" value="Show Obj" onclick="obj.showArray">
<div id="showContent">
</div>
this.saveToArraywhich calls the method saveToArray(). Being part of the constructor I believe the method will take the private variables fname, lname, age, gender and the global variable runningNr. So yes arguments are passed. Except this is a misinterpreting of myself :/ please correct me if so!