I have a problem with my javascript, whenever I send an array to another function (through a parameter) to be used and formatted it somehow returns the formatted array back to the previous function.
https://jsfiddle.net/jvrqt0mg/1/
HTML
<p>Column</p>
<select id='colSelect'></select>
<p>Weight</p>
<select id='weightSelect'></select>
JavaScript
updateTableSettings = function () {
var colAmount = "4";
var colArray = [];
//Updates the array with the dropdown options selected by the user
updateDropdown = function (notWeight) {
//Gets the max amount of weight (The row amount)
var colWeight = colAmount;
var colAmount2 = colAmount;
//Minuses the number selected in the column drop down with weight.
colWeight = colWeight - ($("#colSelect option:selected").text() - 1);
colAmount2 = colAmount2 - ($("#weightSelect option:selected").text() - 1);
//Checks to make sure that it was the column that changed and not the weight drop down
colArray[($("#colSelect option:selected").text()) - 1] = (eval($("#weightSelect option:selected").text()) - 1);
//Resets the weight drop down
$("#weightSelect").find("Option").remove().end();
//Repopulates the weight drop down depending on how much weight is left
for (var i = 0; i < colWeight; i++) {
//Creates a new option in the weight drop down
$("#weightSelect").append(new Option(i + 1, (i + 1)));
}
//This is me passing the array whenever a drop down has been selected or changed
//Sends it to the array to the new function(I want dont want the array in function to be formatted)
format(colArray);
//This shows the array in this function getting formatted
console.log(colArray);
return;
};
//Fills the array
var fillArray = function () {
for (var i = 0; i < colAmount; i++) {
colArray[i] = 0;
}
};
//Removes all the options to make sure they don't double up
$("#colSelect").find("Option").remove().end();
$("#weightSelect").find("Option").remove().end();
//Populates the array
fillArray();
//Creates the options in the drop down
for (var i = 0; i < colAmount; i++) {
//Gives the colArray a default of 0 for every column
$("#weightSelect").append(new Option(i + 1, ("weight" + i)));
$("#colSelect").append(new Option(i + 1, ("opt" + i)));
}
//Checks column drop down for a change
this.$("#colSelect, #weightSelect").change(function (e) {
updateDropdown();
});
};
//This is where the array gets passed into
format = function (array) {
//Formats so it pops the array for every value inside the array
formatArray = function (array) {
var testArray = [];
testArray = array;
for (var i = 0; i < array.length; i++) {
for (var k = 0; k < array[i]; k++) {
//This seems to delete the array in a different function... HOW?~?!!!
testArray.pop();
}
}
return;
};
//alert this
formatArray(array);
//I will have code to draw a table row with columns/weight(Size)
};
I want the original array to stay the same so each time a drop down is changed it resends the array.
Sorry for my bad code, I'm new to JS. Thanks in advance