generate list of variables from a FOR loop - javascript
var select = [];
for (var i = 0; i < nameslots; i += 1) {
select[i] = this.value;
}
This is an extract of my code. I want to generate a list of variables (select1, select2, etc. depending on the length of nameslots in the for.
This doesn't seem to be working. How can I achieve this? If you require the full code I can post it.
EDIT: full code for this specific function.
//name and time slots
function gennametime() {
document.getElementById('slots').innerHTML = '';
var namelist = editnamebox.children, slotnameHtml = '', optionlist;
nameslots = document.getElementById('setpresentslots').value;
for (var f = 0; f < namelist.length; f += 1) {
slotnameHtml += '<option>'
+ namelist[f].children[0].value
+ '</option>';
};
var select = [];
for (var i = 0; i < nameslots; i += 1) {
var slotname = document.createElement('select'),
slottime = document.createElement('select'),
slotlist = document.createElement('li');
slotname.id = 'personname' + i;
slottime.id = 'persontime' + i;
slottime.className = 'persontime';
slotname.innerHTML = slotnameHtml;
slottime.innerHTML = '<optgroup><option value="1">00:01</option><option value="2">00:02</option><option value="3">00:03</option><option value="4">00:04</option><option value="5">00:05</option><option value="6">00:06</option><option value="7">00:07</option><option value="8">00:08</option><option value="9">00:09</option><option value="10">00:10</option><option value="15">00:15</option><option value="20">00:20</option><option value="25">00:25</option><option value="30">00:30</option><option value="35">00:35</option><option value="40">00:40</option><option value="45">00:45</option><option value="50">00:50</option><option value="55">00:55</option><option value="60">1:00</option><option value="75">1:15</option><option value="90">1:30</option><option value="105">1:45</option><option value="120">2:00</option></optgroup>';
slotlist.appendChild(slotname);
slotlist.appendChild(slottime);
document.getElementById('slots').appendChild(slotlist);
(function (slottime) {
slottime.addEventListener("change", function () {
select[i] = this.value;
});
})(slottime);
}
}
You'll have to close in the iterator as well in that IIFE
(function (slottime, j) {
slottime.addEventListener("change", function () {
select[j] = this.value;
});
})(slottime, i);
and it's only updated when the element actually change
The cool thing about JavaScript arrays is that you can add things to them after the fact.
var select = [];
for(var i = 0; i < nameSlots; i++) {
var newValue = this.value;
// Push appends the new value to the end of the array.
select.push(newValue);
}
Related
JS missing ) in parenthetical (line #88)
I'm writing a program in JS for checking equal angles in GeoGebra. This is my first JS code, I used c# formerly for game programming. The code is: var names = ggbApplet.getAllObjectNames(); var lines = new Set(); var angles = new Set(); var groups = new Set(); for(var i=0; i<names.length; i++) { if(getObjectType(names[i].e)==="line") { lines.add(names[i]); } } for(var i=0;i<lines.size;i++) { for(var j=0;j<i;j++) { var angle = new Angle(i,j); angles.add(angle); } } for(var i=0;i<angles.size;i++) { var thisVal = angles.get(i).value; var placed = false; for(var j=0;j<groups.size;j++) { if(groups.get(j).get(0).value===thisVal) { groups.get(j).add(angles.get(i)); placed = true; } } if(!placed) { var newGroup = new Set(); newGroup.add(angles.get(i)); groups.add(newGroup); } } for(var i=0;i<groups.size;i++) { var list=""; for(var j=0;j<groups.get(i).size;j++) { list = list+groups.get(i).get(j).name; if(j != groups.get(i).size-1) { list = list+","; } } var comm1 = "Checkbox[angle_{"+groups.get(i).get(0).value+"},{"+list+"}]"; ggbApplet.evalCommand(comm1); var comm2 = "SetValue[angle_{"+groups.get(i).get(0).value+"}+,0]"; ggbApplet.evalCommand(comm2); } (function Angle (i, j) { this.lineA = lines.get(i); this.lineB = lines.get(j); this.name = "angleA_"+i+"B_"+j; var comm3 = "angleA_"+i+"B_"+j+" = Angle["+this.lineA+","+this.lineB+"]"; ggbApplet.evalCommand(comm3); var val = ggbApplet.getValue(this.name); if(val>180) {val = val-180} this.value = val; ggbApplet.setVisible(name,false) }); function Set { var elm; this.elements=elm; this.size=0; } Set.prototype.get = new function(index) { return this.elements[index]; } Set.prototype.add = new function(object) { this.elements[this.size]=object; this.size = this.size+1; } It turned out that GeoGebra does not recognize Sets so I tried to make a Set function. Basically it collects all lines into a set, calculates the angles between them, groups them and makes checkboxes to trigger visuals. the GeoGebra functions can be called via ggbApplet and the original Workspace commands via ggbApplet.evalCommand(String) and the Workspace commands I used are the basic Checkbox, SetValue and Angle commands. The syntax for GeoGebra commands are: Checkbox[ <Caption>, <List> ] SetValue[ <Boolean|Checkbox>, <0|1> ] Angle[ <Line>, <Line> ] Thank you for your help!
In short, the syntax error you're running to is because of these lines of code: function Set { and after fixing this, new function(index) / new function(object) will also cause problems. This isn't valid JS, you're likely looking for this: function Set() { this.elements = []; this.size = 0; } Set.prototype.get = function(index) { return this.elements[index]; }; Set.prototype.add = function(object) { this.elements[this.size] = object; this.size = this.size + 1; }; Notice no new before each function as well. I'm not sure what you're trying to accomplish by creating this Set object though - it looks like a wrapper for holding an array and its size, similar to how something might be implemented in C. In JavaScript, arrays can be mutated freely without worrying about memory. Here's an untested refactor that removes the use of Set in favour of native JavaScript capabilities (mostly mutable arrays): var names = ggbApplet.getAllObjectNames(); var lines = []; var angles = []; var groups = []; for (var i = 0; i < names.length; i++) { if (getObjectType(names[i].e) === "line") { lines.push(names[i]); } } for (var i = 0; i < lines.length; i++) { for (var j = 0; j < i; j++) { angles.push(new Angle(i, j)); } } for (var i = 0; i < angles.length; i++) { var thisVal = angles[i].value; var placed = false; for (var j = 0; j < groups.length; j++) { if (groups[j][0].value === thisVal) { groups[j].push(angles[i]); placed = true; } } if (!placed) { groups.push([angles[i]]); } } for (var i = 0; i < groups.length; i++) { var list = ""; for (var j = 0; j < groups[i].length; j++) { list += groups[i][j].name; if (j != groups[i].length - 1) { list += ","; } } var comm1 = "Checkbox[angle_{" + groups[i][0].value + "},{" + list + "}]"; ggbApplet.evalCommand(comm1); var comm2 = "SetValue[angle_{" + groups[i][0].value + "}+,0]"; ggbApplet.evalCommand(comm2); } function Angle(i, j) { this.name = "angleA_" + i + "B_" + j; var comm3 = "angleA_" + i + "B_" + j + " = Angle[" + lines[i] + "," + lines[j] + "]"; ggbApplet.evalCommand(comm3); var val = ggbApplet.getValue(this.name); if (val > 180) { val -= 180; } this.value = val; ggbApplet.setVisible(name, false); } Hopefully this helps!
Your function definition is missing the parameter list after the function name. Also, you're initializing the elements property to an undefined value. You need to initialize it to an empty array, so that the add method can set elements of it. function Set() { this.elements=[]; this.size=0; }
How do I get the triangle array outside so that I can use it
I am storing some ID's to an array. The array is correctly populated, but I am not able to log it outside nor use it. I am slightly confused. Here is my code, JavaScript function storeTriangle() { var triangle = []; return function storePolygons() { for (var i = 0; i < 31; i++) { triangle[i] = s.select("#triangle36_" + (224 - i) + "_"); } }; storePolygons(); }; var polygons = storeTriangle(); polygons();
function storeTriangle() { var triangle = []; return function() { for (var i = 0; i < 31; i++) { triangle[i] = s.select("#triangle36_" + (224 - i) + "_"); } return triangle; }; }; var polygons = storeTriangle(); var triangle=polygons(); console.info(triangle)
How to save this in a JS variable?
I would like to know how to save the output of this into a "var a=" navigator.plugins.refresh(false); var numPlugins = navigator.plugins.length; for (var i = 0; i < numPlugins; i++){ var plugin = navigator.plugins[i]; if (plugin) { document.write(plugin.name + plugin.description + plugin.filename) } }
Declare a outside of the loop and define it as an empty string, then append results to it as you go: navigator.plugins.refresh(false); var numPlugins = navigator.plugins.length; var a = ''; for (var i = 0; i < numPlugins; i++){ var plugin = navigator.plugins[i]; if (plugin) { a += plugin.name + plugin.description + plugin.filename; } } You may want to use an array of strings though, since you could have many plugins: navigator.plugins.refresh(false); var numPlugins = navigator.plugins.length; var a = []; for (var i = 0; i < numPlugins; i++){ var plugin = navigator.plugins[i]; if (plugin) { a.push(plugin.name + plugin.description + plugin.filename); } } EDIT If you need to hash a into something: var hash = yourMd5Function(a); Or for the second example: var b = a.join(','); // "plugin1,plugin2,..." for example var hash = yourMd5Function(b);
Spliting String and getting appropriate value in JavaScript
I have a string where |||| means next to it is the directory. ||| means the user is allowed to access this directory and || means the files allocated to these users follow. I need to find allocated file names of a specific user from this string. I have tried to split the string and assign values to an array but I am not able to get the result I'm looking for. This is the string: ||||Root|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,||||1400842226669|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,testTask1_20140528135944.xlsx,testTask2_20140528140033.xlsx,||||1401191909489|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,testTask1_20140528135944.xlsx,testTask2_20140528140033.xlsx,LimitTest_20140528164643.xlsx, And here is my attempt: function getData() { var user = 'km11285c'; var value = "||||Root|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,||||1400842226669|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,testTask1_20140528135944.xlsx,testTask2_20140528140033.xlsx,||||1401191909489|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,testTask1_20140528135944.xlsx,testTask2_20140528140033.xlsx,LimitTest_20140528164643.xlsx,"; var users = null; var files = null; var Dir = value.split("||||"); var arrayLength = Dir.length; for (var i = 0; i < arrayLength; i++) { users = Dir[i].split("|||"); } return users; } console.log(getData()); and the jsFiddle
I changed your jsfiddle example a bit so maybe you need to change the code here and there, but something like this should work: function buildTree(data) { var tree = []; var dirs = data.split("||||"); // Remove the first entry in the array, since it should be empty. dirs.splice(0, 1); for (var i = 0; i < dirs.length; ++i) { var tempArray = dirs[i].split("|||"); var dirName = tempArray[0]; var usersAndFiles = tempArray[1]; tempArray = usersAndFiles.split("||"); var users = tempArray[0]; var files = tempArray[1]; var treeDir = { name: dirName }; treeDir.users = users.split(","); treeDir.files = files.split(","); tree.push(treeDir); } return tree; } function getData() { var user = 'km11285c'; var value="||||Root|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,||||1400842226669|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,testTask1_20140528135944.xlsx,testTask2_20140528140033.xlsx,||||1401191909489|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,testTask1_20140528135944.xlsx,testTask2_20140528140033.xlsx,LimitTest_20140528164643.xlsx,"; var tree = buildTree(value); for (var i = 0; i < tree.length; ++i) { var dir = tree[i]; if (dir.users.indexOf(user) >= 0) { console.log("User '" + user + "' has access to directory '" + dir.name + "', which contains these files: " + dir.files.join(",")); } } } getData();
Why is a named function not working when the unnamed was?
I had this code: $('#testsPane').live("click", function() {//if the secondary ui nav tests is selected //Displays the test list var listOfTests = ""; var subjects = []; var tests= []; var titles = []; var keysplit; var testSubj; var key, value; for (var i = 0; i < localStorage.length; i++) { key = localStorage.key(i); value = localStorage.getItem(key); keysplit = key.split("."); tests.push(value); titles.push(keysplit[0]); subjects.push(keysplit[keysplit.length-1]); } for(var i=0; i < tests.length; i++) { listOfTests += '<div class="testDisplayBox"><div class="subjColorBar"></div><div class="testListIndiContain"><span class="testListTitle">' + titles[titles.length-(i+1)] + '</span><span> in </span><span class="testListSubj">' + subjects[subjects.length-(i+1)] + '</span></div><div class="testListTags"><span class="specTags">quiz</span></div></div>'; } var testsDashboard = '<div id="testsList">' + listOfTests + '</div>'; $('#selectedPane').append(testsDashboard);//adds the html to the pane to make it into the tests dashboard }) The above code worked but I wanted to reuse some of it so I put it into a function. When I did that it did not work. Any idea why? The code below is with using a named function. function grabTestList() {//Displays the test list var keysplit; var testSubj; var key, value; for (var i = 0; i < localStorage.length; i++) { key = localStorage.key(i); value = localStorage.getItem(key); keysplit = key.split("."); tests.push(value); titles.push(keysplit[0]); subjects.push(keysplit[keysplit.length-1]); }} $('#testsPane').live("click", function() {//if the secondary ui nav tests is selected grabTestList(); var listOfTests = ""; var subjects = []; var tests= []; var titles = []; for(var i=0; i < tests.length; i++) { listOfTests += '<div class="testDisplayBox"><div class="subjColorBar"></div><div class="testListIndiContain"><span class="testListTitle">' + titles[titles.length- (i+1)] + '</span><span> in </span><span class="testListSubj">' + subjects[subjects.length-(i+1)] + '</span></div><div class="testListTags"><span class="specTags">quiz</span></div></div>'; } var testsDashboard = '<div id="testsList">' + listOfTests + '</div>'; $('#selectedPane').append(testsDashboard);//adds the html to the pane to make it into the tests dashboard })
Because you are defining variables in the context of the anonymous function which are unknown to the named function. Pass them to grabTestList so the .push methods can mutate those arrays. function grabTestList(tests, titles, subjects) { // manipulate tests/titles/subjects } $('blah').live('click', function() { var tests = [], titles = [], subjects = []; grabTestList( tests, titles, subjects ); // since tests, titles, and subjects are mutated by the function, you can just loop through them here. }) DEMO: Here's a sample version which you can base your code on: http://jsfiddle.net/JLK6N/2/ updated with the fix: http://jsfiddle.net/JLK6N/3/ Remember that objects are passed by reference, arrays are objects, and methods like .push are mutator methods.