I am attempting to count up the index 1 values of the arrays within nested arrays. My desired output is:
[ [ [ 0, 0 ], [ 0, 1], [ 0, 2 ] ] ]
However I am getting the following as my output:
[ [ [ 0, 2 ], [ 0, 2 ], [ 0, 2 ] ] ]
Here is the code I am running, I am assuming the issue falls in the second for loop:
let width = 3
let height = 1
var grid = [];
let y = 0
for(var i=0; i<height; i++) {
grid[i] = new Array(width).fill([0,0])
}
let currentRow = grid[0]
for (let j = 0; j<grid[0].length; j++){
currentRow[j][1] = j
}
Thanks for helping!
change your code to
let width = 3;
let height = 1;
var grid = [];
let y = 0;
for (var i = 0; i < height; i++) {
grid[i] = new Array(width);
for (let j = 0; j < grid[i].length; j++) {
grid[i][j] = [0, 0];
}
}
let currentRow = grid[0];
for (let j = 0; j < grid[0].length; j++) {
currentRow[j][1] = j;
}
when you use grid[i] = new Array(width).fill([0,0]) you are saving one instance of [0,0] in your grid[i] array ,it means currentRow[0]==currentRow[1] is true, so when you change the currentRow[j][1] all of currentRow elements get changed, in your last step of second loop you change currentRow[2][1]=2 so all of currentRow elements get changed in this way.
Related
I need some help with my code. I try to insert only one value in each array. I need to fill in the row first and after that, if the row is full then move to the next column. I try to solve this part for a couple of days but I fail. So here is my code
const testData = [1,2,3,4,5,6,7,8,9];
const create2dArray = (row, column) => {
var result = [];
for(let i = 0; i< row; i++){
result[i]= [];
for(let j = 0; j<column; j++){
result[i][j] = [];
for(let e = 0; e < testData.length; e++){
result[i][j] = [testData[e]];
}
}
}
return result;
}
let Column = 5
let Row = 5
filterQuotaData(EnrollmentQuota);
var ground = create2dArray(Column,Row);
console.log(ground);
Suppose the output is :
[1],[2],[3],[4],[5]
[6],[7],[8],[9],[]
[],[],[],[],[]
[],[],[],[],[]
[],[],[],[],[]
instead, I got:
[9],[9],[9],[9],[9]
[9],[9],[9],[9],[9]
[9],[9],[9],[9],[9]
[9],[9],[9],[9],[9]
[9],[9],[9],[9],[9]
I hope someone can help me to solve this problem
Following code
const testData = [1,2,3,4,5,6,7,8,9];
const create2dArray = (row, column) => {
var result = [];
k = 0
for(let i = 0; i< row; i++){
result[i]= [];
for(let j = 0; j<column; j++){
if(k < testData.length) {
result[i][j] = [testData[k]];
} else {
result[i][j] = [];
}
k++
}
}
return result;
}
let Column = 5
let Row = 5
//filterQuotaData(EnrollmentQuota);
var ground = create2dArray(Column,Row);
console.log(ground);
produces
[
[ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ] ],
[ [ 6 ], [ 7 ], [ 8 ], [ 9 ], [] ],
[ [], [], [], [], [] ],
[ [], [], [], [], [] ],
[ [], [], [], [], [] ]
]
Does it what you need?
What's happening in your code is that you have the 3rd loop adding everything to each column from 2nd loop. The reason why they are all 9s is because you are overwriting each column by using an assignment instead of adding it to the array:
// 3rd loop
array[0][0][0] = 1 // 1st iteration [[[1],...]]
array[0][0][1] = 2 // 2nd iteration [[[2],...]]
Here's an example that uses 2 loops and pushes sub-arrays and shifts from test array.
const test = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const create2dArray = (row, column) => {
let result = [];
for (let r = 0; r < row; r++) {
result.push([]);
for (let c = 0; c < column; c++) {
let data = test.length < 1 ? [] : [test.shift()]
result[r].push(data);
}
}
return result;
}
let row = 5, col = 5;
let ground = create2dArray(row, col);
console.log(JSON.stringify(ground));
I am trying to make nested arrays that mimic a grid using for loops. I want it to look like this:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
I have been able to create one line of the array like so
[0, 0, 0]
using
var num = 3;
var arr = [];
for (var j = 0; j < num; j++) {
arr[j] = 0;
}
console.log(arr);
I stuck with how to implement nesting the second for loop around the first. My thought would be to push the finished output of the inner loop to the i index of of the outer loop. So something like
var num = 3;
var arr = [];
for (var i = 0; i < num; i++) {
for (var j = 0; j < num; j++) {
arr[j] = 0;
}
// some command here to push the completed output of the inner output to i index of the outer loop
}
console.log(arr);
I am on the right track with this? If not, where do I need to shift my thinking here? I haven’t been able to get a solution that produces the desired outcome yet.
You just needed to initialize the subarray for each set, then utilize both counters to place your '0'
var num = 3;
var arr = [];
for (let i = 0; i < num; i++) {
arr[i] = [];
for (let j = 0; j < num; j++) {
arr[i][j] = 0;
}
}
console.log(arr);
I'm trying to make my 2d matrix to have numbers which continue on the new row
var myMatrix = [];
var row = 5;
var colom = 3;
for (var i = 0; i < rows; i++) {
var toto = 1;
myMatrix[i] = [i];
for (var j = 0; j < colom; j++) {
myMatrix[i][j] = [i + j];
}
}
console.log(myMatrix);
I'm trying to make it print numbers like this:
123
456
789 and etc...
but without success:/
can someone help and also give a video or site with examples where i can learn more about that kind of stuff?
First, a look at what your code is doing:
const myMatrix = [];
const rows = 5;
const columns = 3;
for (var i = 0; i < rows; i++) {
myMatrix[i] = [i];
for (var j = 0; j < columns; j++) {
myMatrix[i][j] = [i+j];
}
}
console.log(myMatrix);
You have a typo in your row/rows variable name. Ignoring that though...
Your myMatrix[i] line is creating an array at i, which is then being set to an array with a value of i. Just this creates a wonky mash-up , where each "row" gets an array with it's row number as the first value, something like this:
[[0], [1], [2], [3], [4]]
Your inner loop then adds a value to that array at the place and adds i+j together, but puts that inside of an array, which isn't what you want, so you get something like this:
[
[[0], [1], [2]], // i = 0
[[1], [2], [3]], // i = 1
[[2], [3], [4]], // i = 2
// ... etc
]
Also note that you are replacing that first [i] anyways, so don't set it like that, just make it an empty array [].
What you want is something like this:
const myMatrix = [];
const rows = 5;
const columns = 3;
for (var i = 0; i < rows; i++) {
myMatrix[i] = [];
for (var j = 0; j < columns; j++) {
myMatrix[i][j] = (i*columns)+j;
}
}
console.log(myMatrix);
There were three changes to your code:
Make the [i] and []. It doesn't hurt anything, but [i] also doesn't make sense.
Take the i+j part out of the array, you just want a value there.
When you add i, multiply it by columns so it doesn't reset every time: (i*columns)+j
This will give you a nice output, starting with 0. If you want it start at 1, just add one to your value:
const myMatrix = [];
const rows = 5;
const columns = 3;
for (var i = 0; i < rows; i++) {
myMatrix[i] = [];
for (var j = 0; j < columns; j++) {
myMatrix[i][j] = (i*columns)+j+1;
}
}
console.log(myMatrix);
Use i * columns + j ... and I have to add up to 30 chars for padding
I have a translation matrix in javascript which I can't seem to get working. If I have a single matrix I can access the different levels OK using the method I have used but as soon as I try to multiply it with another matrix I get an error: Internal error setting variable. Thanks for your comments.
function funct(number, number1) {
var matrixATranslate = new Array();
var matrixBTranslate = new Array();
var matrixCTranslate = new Array();
var matrixATranslate = [
[1, number, 0, number1],
[number1, 1, 0, 0],
[0, 0, 0, 0]
];
var matrixBTranslate = [
[number],
[number1],
[0],
[1]
];
var row = 3, col = 4, add = 4;
for (var i = 0; i < row; i++) {
for (var j = 0; j < col; j++) {
var sum = 0;
for (var k = 0; k < add; k++) {
sum += matrixATranslate[i, k] * matrixBTranslate[k, j];
}
matrixCTranslate[i, j] = sum;
}
}
Vars.transformedPoint.x = matrixCTranslate[0][0];
Vars.transformedPoint.y = matrixCTranslate[0][0];
return Vars.transformedPoint;
}
Vars.transformedPoint = funct(10, 20);
Got the working code here.
var matrixATranslate = new Array();
var matrixBTranslate = new Array();
var matrixCTranslate = new Array();
for(var i = 0; i < 4; i++) {
for(var j = 0; j < 4; j++) {
matrixCTranslate[i,j] = [ ];
}
}
var matrixATranslate = [
[5,6,7,8],
[10,20,30,40],
[100,200,300,400]
];
var matrixBTranslate = [5,6,7,8];
matrixCTranslate[0][0]=matrixATranslate[2][3] * matrixBTranslate[0,0];
var varA=matrixCTranslate[0,0];
console.log(varA)
this code http://jsfiddle.net/minagabriel/5MQ77/
var flowShadeBigArray =[] ;
var x = [7, 411, 780]
var y = [286, 712, 1058]
for( var i = 0 ; i< x.length;i++){
for(var index = x[i]; index <= y[i] ; index++ ){
var temp = [] ;
temp.push(index) ;
flowShadeBigArray.push(temp);
}
}
console.log(JSON.stringify(flowShadeBigArray));
generate the following array
[[7],[8],[9],[10],[11],[12],[13],[14]................[1056],[1057],[1058]]
i want to create a three arrays inside flowShadeBigArray and have the [x[i] ... y[i] ]
grouped together:
example
[ [ [7]....[286] ] , [ [411]...[712] ] ,[ [780]...[1058] ] ]
NOTE i still need to keep each of these numbers as an array so i can use it an an index for something else
THANKS
Just move the temp initialization to the first loop, before the second, and the .push() to the first loop after the second (or before, doesn't matter).
var flowShadeBigArray = [];
var x = [7, 411, 780]
var y = [286, 712, 1058]
for (var i = 0; i < x.length; i++) {
var temp = [];
for (var index = x[i]; index <= y[i]; index++) {
temp.push(index);
}
flowShadeBigArray.push(temp);
}
http://jsfiddle.net/5MQ77/1/
If each individual number belongs in its own Array, then change this:
flowShadeBigArray.push(temp);
to this:
flowShadeBigArray.push([temp]);
-edited
function range(from, to) {
var arr = [];
while (from <= to) {
arr.push([i++]);
}
return arr;
}
var result = [];
for (var i=0; i<x.length; i++) {
result.push(range(x[i], y[i]));
}