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);
Related
This function takes in two arrays, arrOne, which is an array of arrays of numbers, and arrTwo, which is an array of numbers. I am trying to make it produce every combination and then eventually every unique combination, however I am facing an issue.
function multOrdProduct(arrOne,arrTwo){
let backSet = [];
let count = 0;
let tempArr = new Array(arrOne.length);
let permArrOne = [];
let permArrTwo = [];
let pushed;
let setPart = [];
for(i=0; i < arrOne.length; i++){
permArrOne.push(arrOne[i]);
}
// ^sets permArrOne to equal the same array as ArrOne
for(i = 0; i < arrOne.length;i++){
for(j = 0; j < arrTwo.length; j++){
setPart = permArrOne[i];
console.log(permArrOne[i]);
setPart.push(arrTwo[j]);
backSet.push(setPart);
}
}
return backSet;
}
I would have thought that permArrOne[i] would not change as I do not set any value to it, however why does it change with each loop?
As youre just referencing permArrOne in setPart and not cloning it, youre changing permArrOne as youre changing setPart.
.slice() is one way to reference a new array:
function multOrdProduct(arrOne,arrTwo){
let backSet = [];
let count = 0;
let tempArr = new Array(arrOne.length);
let permArrOne = [];
let permArrTwo = [];
let pushed;
let setPart = [];
for(i=0; i < arrOne.length; i++){
permArrOne.push(arrOne[i]);
}
// ^sets permArrOne to equal the same array as ArrOne
for(i = 0; i < arrOne.length;i++){
for(j = 0; j < arrTwo.length; j++){
setPart = permArrOne[i].slice();
console.log(permArrOne[i]);
setPart.push(arrTwo[j]);
backSet.push(setPart);
}
}
return backSet;
}
let arr1 = [[0, 1], [1, 2]]
let arr2 = [0, 1, 2]
multOrdProduct(arr1, arr2)
I'm working with the problem below:
You are provided with an empty array called nestedArr. Using a for loop starting at index 0, add 5 subarrays to nestedArr, with each nested array containing the string 'loop' concatenated with the corresponding index in nestedArr as its first element, and the index as its second element.
Example of a subarray: ['loop3', 3].
This is what I've tried
const nestedArr = [];
for (var i = 0; i < 5; i++) {
nestedArr[i] = []
for(var j = 0; j < 2; j++) {
nestedArr[i][j] = 'loop'+[i], i
}
}
console.log(nestedArr)
It gives me [['loop0', 'loop0'], ['loop1', 'loop1'], ['loop2', 'loop2'], ['loop3', 'loop3'], ['loop4', 'loop4']]
Can't figure out how to have it in this format: ['loop3', 3].
Please guide me!
You can just construct the array literally or use a ternary operator to check the first index in the inner loop and concatenate the string loop
const nestedArr = [];
for (var i = 0; i < 5; i++) {
nestedArr[i] = ['loop' + i, i];
}
const nestedArr2 = [];
for (var i = 0; i < 5; i++) {
nestedArr2[i] = [];
for(let j = 0; j < 2; j++){
nestedArr2[i][j] = j == 0 ?'loop' + i : i;
}
}
console.log(nestedArr, nestedArr2)
const nestedArr = [];
for (let i = 0; i < 5; i++) {
nestedArr.push(['loop' + i, i]);
}
console.log(nestedArr);
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 want to create a multidimensionnale array using a for loop.
The result i need is :
[
[1, 2, 3],
[4,5,6],
[7,8,9]
]
So how can i loop from 1 to 10 and each 3 numbers i create a new array ?
I don't find a solution.. Thanks a lot for your help
This should be what you're looking for:
var outer = [];
var inner = [];
for (var i = 1; i < 10; i++) {
inner.push(i);
if (inner.length == 3) {
outer.push(inner);
inner = [];
}
}
if (inner.length > 0) outer.push(inner);
console.log(outer);
Remove the if after the loop, if you don't want any elements with less than 3 inner elements.
This might be the simplest solution:
const result = [];
for(let i = 1; i < 10; i = i + 3) {
result.push([i, i+1, i+2]);
}
console.log(result);
If you're sure you want 3 consecutive numbers, you can loop from 1 to 10 skipping 3 every time, so your i inside for loop would be 1, then 4 and then 7 in last iteration. With every iteration you create [i, i+1, i+2] array on the spot and push it to result array. However this solution is based on above conditions. It works well for 10, for any number it would require additional if statement inside for loop.
You could take a single loop and use a variable for the first index of the 2D array.
var i, r, array = [];
for (i = 0; i < 9; i++) {
r = Math.floor(i / 3);
array[r] = array[r] || [];
array[r].push(i + 1);
}
console.log(array);
var c = 1;
for(var i = 0; i < 3; i++)
for(var j = 0; j < 3; j++)
array[i][j] = ++c;
I have got a little function in javascript and I want to split an array A into a 2d array.
I will be used for square matrices. Obviously,I want it to be 2x2 if a square matrix of 2x2 is in the input and so on for 3x3 and. But I'm stuck after having read a first row.So my arr rows are repeated. Does anyone have any ideas about how I can get the next rows read properly.So,for instance,lets say I do have an array
A = [2,1,4,5,1,2,3,1,9]
Then I want my array arr to look like this:
arr = [[2,1,4],[5,1,2],[3,1,9]]
This will later be used for calculation a determinant of a matrix.
function create2Darray(clname) {
var A = document.getElementsByClassName(clname);
var arr = new Array();
var rows = Math.sqrt(A.length);
for (var i = 0; i < rows; i++) {
arr[i] = new Array();
for (var j = 0; j < rows; j++) {
arr[i][j] = A[j].value;
}
}
}
You are assigning always the same value. It should be something like:
arr[i][j] = A[i*rows+j].value;
EDIT: Here's a complete function without the DOM manipulation (that is, A is a simple array of integers):
function create2Darray(A) {
var arr = [];
var rows = Math.sqrt(A.length);
for (var i = 0; i < rows; i++) {
arr[i] = [];
for (var j = 0; j < rows; j++) {
arr[i][j] = A[i * rows + j];
}
}
return arr;
}
console.log(create2Darray([1, 2, 3, 4, 5, 6, 7, 8, 9]))