translation matrix in javascript - javascript

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)

Related

Counting up with nested arrays

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.

for loops nested array output

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);

Array Transpose: Error reading property from "Undefined"

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;
}

selection sort in javascript via Khan Academy

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;

Javascript can't convert undefined to object

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
}

Categories