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.
Part of my homework I have to write a program that calculates all multiplication tables up to 10 and store the results in an array. The first entry formatting example is "1 x 1 = 1".
I think I have my code written right for the nested for loop but I'm not sure on how to output it properly.
var numOne = [1,2,3,4,5,6,7,8,9,10];
var numTwo = [1,2,3,4,5,6,7,8,9,10];
var multiple = [];
for (var i = 0; i < numOne.length; i++) {
for (var j = 0; j < numTwo.length; j++) {
multiple.push(numOne[i] * numTwo[j]);
console.log(numOne[i] * numTwo[j]);
}
}
You can use a template string, and you can just loop through the numbers in the arrays without using arrays (in the same way you were looping through the indices):
var multiple = [];
var m;
for (var i = 1; i <= 10; i++) {
for (var j = 1; j <= 10; j++) {
m = i * j;
multiple.push(m);
console.log(`${i} * ${j} = ${m}`);
}
}
var multiple = [];
var first = 1;
var last = 10;
for (var i = first; i <= last; i++) {
for (var j = first; j <= last; j++) {
multiple.push(i + " x " + j + " = " + (i*j));
console.log(multiple[multiple.length-1]);
}
}
Not sure if ES6 is part of your curriculum, so here is how to do it with and without template literals
// Create the arrays that you want to multiply
var numOne = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var numTwo = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Create a function that accepts both arrays as arguments
function multiply(arr1, arr2) {
var products = [];
for (var i = 0; i < arr1.length; i++) {
for (var j = 0; j < arr2.length; j++) {
//Here we are using template literals to format the response, so that the program will show you the inputs and calculate the answer
products.push(`${arr1[i]} X ${arr1[j]} = ${arr1[i] * arr2[j]}`);
/* If ES6 is outside of the curriculum, the older method for formatting would be like this:
products.push(arr1[i] + " X " + arr2[j] + " = " + arr1[i]*arr2[j])
*/
}
}
console.log(products);
return products;
}
// Call the second function example
multiply(numOne, numTwo);
Getting an error message for the below code that works to get Values from several cells in my order entry tab named POTemplate and log them in my POHistory tab the serves to compile a list of all order detail entries. As the debugger gets to the bottom of the below code, I get an error message stating: "Cannot Read Property 0.0 from Undefined"
function submit() {
var app = SpreadsheetApp;
var tplSheet = app.getActiveSpreadsheet().getSheetByName("POTemplate");
var tplFRow = 22, tplLRow = tplSheet.getLastRow();
var tplRowsNum = tplLRow - tplFRow + 1;
var tplFCol = 1, tplLCol = 16;
var tplColsNum = tplLCol - tplFCol + 1;
var rangeData = tplSheet.getRange(22, 1, 5, 15).getValues();
var colIndexes = [0, 3, 10, 12, 15];
var fData = filterByIndexes(rangeData, colIndexes);
var target = "POHistory";
var targetSheet = app.getActiveSpreadsheet().getSheetByName(target);
var tgtRow = targetSheet.getLastRow() + 1;
var tgtRowsNum = fData.length - tgtRow + 1;
var tgtCol = 1;
var tgtColsNum = fData[0].length - 1 + 1;
targetSheet.getRange(tgtRow, tgtCol, tgtRowsNum,
tgtColsNum).setValues(fData);
}
function filterByIndexes(twoDArr, indexArr) {
var fData = [];
twoDArr = twoDArr.transpose();
for(var i = 0; i < indexArr.length; i++) {
fData.push(twoDArr[indexArr[i]]);
}
return fData.transpose();
}
Array.prototype.transpose = function() {
var a = this,
w = a.length ? a.length : 0,
h = a[0] instanceof Array ? a[0].length : 0;
if (h === 0 || w === 0) {return [];}
var i, j, t = [];
for (i = 0; i < h; i++) {
t[i] = [];
for (j = 0; j < w; j++) {
t[i][j] = a[j][i];
}
}
return t;
}
I am not sure what Im doing wrong in my code, but it sorts the numbers correctly, but also leaves this output:
Array after sorting: ,,,,,,,7,,9,,11,,,,,,,,,,,22,,,,,,,,,,,,,,,,,,,,42,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,88,,,,,,,,,,,99
Please help me troubleshoot my code!
var swap = function(array, firstIndex, secondIndex) {
var temp = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = temp;
};
var indexOfMinimum = function(array, startIndex) {
var minValue = array[startIndex];
var minIndex = startIndex;
for(var i = minIndex + 1; i < array.length; i++) {
if(array[i] < minValue) {
minIndex = i;
minValue = array[i];
}
}
return minIndex;
};
var selectionSort = function(array) {
var length = array.length;
for(var i = 0; i < length; i++){
var min = indexOfMinimum(array,array[i]);
swap(array, i, min);
}
};
var array = [22, 11, 99, 88, 9, 7, 42];
selectionSort(array);
println("Array after sorting: " + array);
Program.assertEqual(array, [7, 9, 11, 22, 42, 88, 99]);
You don't want the value at index i, you just want i itself.
var min = indexOfMinimum(array, array[i]);
should be
var min = indexOfMinimum(array, i);
var selectionSort = function(array) {
var length = array.length;
for(var i = 0; i < length; i++){
var min = indexOfMinimum(array,i);
swap(array, i, min);
}
}
Try this:
var selectionSort = function(array) {
var minIdx;
for(var i = 0; i < array.length - 1; i++){
minIdx = indexOfMinimum(array, i);
swap(array, minIdx, i);
}
return array;
I have the following code
for(i = 0; i < num; i++) {
var yPos = 10*i;
var numCells = wid/30;
for(j = 0; j < numCells; j++) {
blocks[i][j] = 1;
}
}
With
blocks = new Array();
However, when I execute the code I receive an error stating that:
can't convert undefined to object
Any ideas? :/
var blocks = [];
for(i = 0; i < num; i++) {
var yPos = 10*i;
var numCells = wid/30;
blocks[i] = []; // here is a fix
for(j = 0; j < numCells; j++) {
blocks[i][j] = 1;
}
}
In your particular case, since all the rows are initialised to be the same (a series of 1s), you can also do
var blocks = new Array(),
blockRow = new Array();
for (var i = 0; i < numCells; i++) {
blockRow.push(1);
}
for (var i = 0; i < num; i++) {
blocks.push(blockRow.slice()); // slice() makes a copy of blockRow
}