I try to create a 4-dimensional array. I fill it dynamically and use content of it in another function. But the content is empty. Is there error below code?
var datas = []; // day number of a week
for(var i = 0; i < 7; i++) {
var size = 24*60/timeInterval;
datas[i] = [];
for(var j = 0; j < size; j++) {
var size2 = allCoords.length / 2;
datas[i][j] = [];
for(var k = 0; k < size2; k++) {
datas[i][j][k] = [];
}
}
}
I test below example :
function foo1()
{
datas[0][0][0].push(10);
}
function foo2()
{
document.getElementByID('result').innerHTML = datas[0][0][0];
}
I see only ,,,,,,,.
I think the principal problem is that you're getting the element where you want to show your result badly using getElementByID instead of getElementById. Also make sure that your element has innerHTML property to write the result, or alternatively use value.
I write the follow example using <textArea id="result"></textArea> and generating a button which calls foo1();foo2(); onClick an it works for me.
In the sample I use an random value for timeInterval and allCoords.length.
Note also that you want a 4-dimensional array however you're creating a 3-dimensional.
var timeInterval = 60;
var allCoords = { length : 1};
var datas = []; // day number of a week
for(var i = 0; i < 7; i++) {
var size = 24*60/timeInterval;
datas[i] = [];
for(var j = 0; j < size; j++) {
var size2 = allCoords.length / 2;
datas[i][j] = [];
for(var k = 0; k < size2; k++) {
datas[i][j][k] = [];
}
}
}
function foo1()
{
datas[0][0][0].push(10);
}
function foo2()
{
document.getElementById('result').value = datas[0][0][0];
}
<textArea id="result"></textArea>
<input type="button" value="foo" onclick="foo1();foo2();"/>
Hope this helps,
Related
i have code like this in actionscript3,
var map: Array = [
[[0,1,0],[0,1,0]],
[[0,1,0], [0,1,0]]];
var nom1: int = 0;
var nom2: int = 0;
var nom3: int = 1;
var nom4: int = 18;
stage.addEventListener (Event.ENTER_FRAME, beff);
function beff (e: Event): void
{
map[nom1][nom2][nom3] = nom4
}
stage.addEventListener (MouseEvent.CLICK, brut);
function brut(e: MouseEvent):void
{
trace (map)
}
when run, it gets an error in its output
what I want is to fill in each "1" value and not remove the "[" or "]" sign
so when var nom1, var nom2 are changed
Then the output is
[[[0,18,0],[0,18,0]],
[[0,18,0],[0,18,0]]]
please helps for those who can solve this problem
If what you want to achieve is to replace every 1 by 18 in this nested array, you could try :
for (var i = 0; i < map.length; i++) {
var secondLevel = map[i];
for (var j = 0; j < secondLevel.length; j++) {
var thirdLevel = secondLevel[j];
for (var k = 0; k < thirdLevel.length; k++) {
if (thirdLevel[k] === 1) {
thirdLevel[k] = 18;
}
}
}
}
Note that, this would only work for nested arrays with 3 levels of depth
var divs = [];
for (var x = 0; x <= nodeArray.length; x++) {
for (var q = 0; q <= nodeArray.childElementCount; q++) {
divs[x][q] = nodeArray[x].childNodes[q].childNodes[0]
};
};
I need a two dimensional array of the child of every child of a div for multiple divs.
Can't get it to work ;c
Anyone knows if this is even possible with javascript?
Thanks.
Edit:
Thanks guys, it works now! :D
I ended up doing it like this:
var divs = [];
nodeArray.forEach(function(array, index) {
divs[index] = nodeArray[index].children;
});
var imgs = [];
for (var x = 0; x < divs.length; x++) {
imgs[x] = [];
for (var y = 0; y < divs[x].length; y++) {
imgs[x][y] = divs[x][y].childNodes[0];
}
}
Using your feedback it works now! Really really really thanks! ;D
Arrays are 0-indexed, This means that the last element of an array is not at array.length but at array.length - 1.
You'll have to create a new array (sub-array) and then start adding elements to it.
Your code should be like this:
var divs = [];
for (var x = 0; x < nodeArray.length; x++) {
// here ^^^
divs[x] = []; // create a new sub-array (or divs.push([]);)
for (var q = 0; q < nodeArray.childElementCount; q++) {
// here ^^^
divs[x][q] = nodeArray[x].childNodes[q].childNodes[0];
};
};
var divs = [];
for (var x = 0; x <= nodeArray.length; x++) {
divs[x] = [];
for (var q = 0; q <= nodeArray[x].length; q++) {
divs[x][q] = nodeArray[x][q][0];
};
};
I'm trying to get the following code to add each number in the element separately and not the whole array together but the dash seems to stop the loop from calculating the total sum of each element. I can't seem to make it so it'll except any length of number for the variable. Any help is greatly appreciated!
var creditNum = [];
creditNum[0] = ('4916-2600-1804-0530');
creditNum[1] = ('4779-252888-3972');
creditNum[2] = ('4252-278893-7978');
creditNum[3] = ('4556-4242-9283-2260');
var allNum = [];
var total = 0;
var num = 0;
var cnt = 0;
for (var i = 0; i < creditNum.length; i++) {
num = creditNum[i];
for (var j = 1; j <= num.length; j++) {
var num = creditNum[i].substring(cnt, j);
console.log(creditNum[i].charAt(cnt));
console.log(cnt, j);
cnt = cnt + 1;
}
if (num != "-") j = j++;
console.log(parseInt(num));
}
console.log(total);
Assuming the intent is to add '4916-2600-1804-0530' and output the value as 49, then the following modification will achieve that.
var creditNum = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978','4556-4242-9283-2260'];
for (var i = 0; i < creditNum.length; i++) {
var num = creditNum[i].replace(/\-/g, '');
var total = 0;
for (var j = 0; j < num.length; j++) {
total += Number(num[j]);
}
console.log(creditNum[i], total);
}
Using native array methods, the code can be refactored as the following.
var creditNumbers = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978','4556-4242-9283-2260'];
creditNumbers.forEach(function(creditNumber) {
var num = creditNumber.replace(/\-/g, '').split('');
var total = num.reduce(function(tally, val) {
return tally += Number(val);
}, 0);
console.log(creditNumber, total);
});
I am trying to produce an array by drawing data from two separate databases. I am getting close, but right now the data is output as one string: e.g.
[Smith, [ED-100,Some ClassED-200,Some Other Class]]
I would like the data to be in the form
[Smith, [[ED-100,Some Class], [ED-200,Some Other Class]]]
I have been spending hours fiddling with the code, but seem to have come up short. Here is what I have:
var teacherzCourses = [];
var teacherz = Object.getOwnPropertyNames(uniqTeach).sort();
for (var j = 0; j < teacherz.length; j++) {
var tName;
var tCourses = [];
for (k = 0; k < registrarData.length; k++) {
Object.getOwnPropertyNames(uniqTeach).sort();
// get the courses each teacher does
for (var j = 0; j < teacherz.length; j++) {
tName = teacherz[j];
tCourses = [];
tempArray = [];
for (k = 0; k < registrarData.length; k++) {
if (registrarData[k].Teacher.indexOf(teacherz[j]) > -1) {
console.log([teacherz[j], registrarData[k].CourseNum, registrarData[k].CourseName]);
tCourses += [registrarData[k].CourseNum, registrarData[k].CourseName];
};
tempArray += (tCourses);
};
teacherzCourses.push([tName, tCourses]);
};
};
console.table(teacherzCourses);
console.log(teacherzCourses[0][1]);
};
I have the feeling I am making this much more complicated than it needs to be.
Change this line:
tCourses += [registrarData[k].CourseNum, registrarData[k].CourseName];
to this:
tCourses.push([registrarData[k].CourseNum, registrarData[k].CourseName]);
As jfriend00 mentioned, there's no += operator on arrays.
Is something wrong with my code? I was expecting my code:
years=new Array();
for (i = 0; i < 5; ++i) {
for (j = 1; j < 13; ++j) {
player.push(Math.round( nestedData[i].value[j] ))
}
years.push(player)
}
console.log(years)
to print something like:
[array[12],array[12],array[12],array[12]]
but the result that i get is:
[array[60],array[60],array[60],array[60]]
Create a new player array inside the first for loop. The problem with your code is that the values were being pushed into the same array instance.
var years = [];
for (i = 0; i < 5; ++i) {
var player = [];
for (j = 1; j < 13; ++j) {
player.push(Math.round( nestedData[i].value[j] ))
}
years.push(player)
}
console.log(years)
As an addition to the correct answer already, please use var to declare your variables:
for (var i=0; i < 5; ++i) {
var player = [];
for (var j = 1; j < 13; ++j) {
...
Otherwise, it will use i as a global variable, which could end poorly if you have two functions looping at the same time, e.g.:
function loopone() {
//wouldn't expect this to be an infinite loop eh?
for (i=0; i < 100; i++) {
looptwo();
}
}
function looptwo() {
for (i=0; i < 10; i++) {
}
}