JavaScript/jQuery - Array list does not go across whole board - javascript

So, I'm working with A* Pathfinding. I got it working however it doesn't work all the way. It works all the way until the last 4 columns to the right. Weird.
It works all the way until X is 10 or less. Which is weird because Y's max is 10. Maybe it's together? I don't know. But my map is 15 columns by 10 rows. Here is an online example: http://mystikrpg.com/html5/
An interesting error I get is Uncaught TypeError: Cannot read property '8' of undefined.
The 8 is the Y of where you clicked. If you click a the very first gray block on the right-side (since row 0 is walled off). Then that 8 would say 1.
Here's the part where it lays out the nodes.
// Creates a Graph class used in the astar search algorithm.
function Graph(grid) {
var nodes = [];
var row, rowLength, len = grid.length;
for (x = 0; x <= 10; x++) {
row = grid[x];
nodes[x] = new Array(15);
for (y = 0; y <= 15; y++) {
nodes[x][y] = new GraphNode(x, y, row[y]);
}
}
this.input = grid;
this.nodes = nodes;
}
However, you can download it offline and put it on localhost, if you'd like here http://mystikrpg.com/html5/Ethios.rar
Anyway... something else I found:
My loadMap() function returns an array of 11 elements.
And when x_block is 13 (clicking on X axis of map) for example, graph.nodes[x_block][y_block] returns undefined.
Here is my loadMap() function:
function loadMap(map) {
if (map == 1) {
return [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 1],
[1, 13, 1, 13, 13, 13, 13, 13, 1, 1, 1, 1, 1, 13, 13, 1],
[1, 13, 1, 1, 13, 1, 1, 13, 1, 13, 13, 1, 13, 13, 13, 1],
[1, 13, 13, 1, 1, 1, 13, 13, 1, 13, 13, 1, 1, 1, 13, 1],
[1, 13, 13, 1, 13, 1, 13, 13, 13, 13, 13, 1, 13, 13, 13, 1],
[1, 13, 13, 13, 13, 1, 13, 13, 13, 13, 13, 1, 13, 13, 13, 1],
[1, 13, 1, 13, 13, 13, 13, 13, 1, 1, 1, 1, 13, 13, 13, 1],
[1, 13, 1, 1, 1, 1, 13, 13, 13, 13, 1, 13, 13, 13, 13, 1],
[1, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]];
}
}
As you can see, it's 15 columns across and 10 rows down.
What am I doing wrong?
UPDATE NEW
for (y = 0; y <= 10; y++) {
row = grid[y];
nodes[y] = new Array(15);
for (x = 0; x <= 15; x++) {
console.log("X: " + x + " Y: " + y);
//console.log("Row: " + row[x]);
nodes[x][y] = new GraphNode(x, y, row[x]);
}
}

You have your x and y nomenclature the wrong way around.
The x axis should be the second dimension in your table, e.g. nodes[y][x]:
for (y = 0; y <= 10; x++) {
row = grid[y];
nodes[y] = [];
for (x = 0; x <= 15; x++) {
nodes[y][x] = new GraphNode(x, y, row[y]);
}
}

Related

Mutating elements in an array in Javascript

Ok so I want to subtract 9 to numbers(elements in an array) over 9:
finalCredDigits = [10, 8, 18, 9, 16, 6, 16, 5, 14, 3, 14, 6, 10, 5, 8]
Thats what I tried
finalCredDigits.forEach(arr =>{
if(arr > 9){
finalCredDigits.push(arr - 9);
}
});
Output = [10, 8, 18, 9, 16, 6, 16, 5, 14, 3, 14, 6, 10, 5, 8, 1, 9, 7, 7, 5, 5, 1]
I know its cuz the result is being pushed in the array but i want to mutate it and replace the answer with numbers over 9
If you want a similar but new array, you should use Array.map():
const finalCredDigits = [10, 8, 18, 9, 16, 6, 16, 5, 14, 3, 14, 6, 10, 5, 8];
const newFinalCredDigits = finalCredDigits.map(v => {
if (v > 9) {
return v - 9;
} else {
return v;
}
});
console.log(newFinalCredDigits.join());
console.log("Is new array?", newFinalCredDigits !== finalCredDigits);
If you want to mutate the array itself with Array.forEach(), you should use the callback's additional parameters:
const finalCredDigits = [10, 8, 18, 9, 16, 6, 16, 5, 14, 3, 14, 6, 10, 5, 8];
finalCredDigits.forEach((v, i, array) => {
if (v > 9) {
array[i] = v - 9;
}
});
console.log(finalCredDigits.join());
But functional programming usually implies preferring immutable over mutable states, so if your array is shared and we'd mutate it with Array.forEach(), it would be considered a code smell. Therefore I'd use a regular for-loop instead:
const finalCredDigits = [10, 8, 18, 9, 16, 6, 16, 5, 14, 3, 14, 6, 10, 5, 8];
{
const l = finalCredDigits.length;
for (let i = 0; i < l; ++i) {
if (finalCredDigits[i] > 9) finalCredDigits[i] -= 9;
}
}
console.log(finalCredDigits.join());
You need to assign to the array index to replace the element, not push a new element.
forEach() passes a second argument to the callback function, containing the current index. You can use this for the assignment.
const finalCredDigits = [10, 8, 18, 9, 16, 6, 16, 5, 14, 3, 14, 6, 10, 5, 8];
finalCredDigits.forEach((el, i) =>{
if(el > 9){
finalCredDigits[i] = el - 9;
}
});
console.log(finalCredDigits);
Try this
let finalCredDigits = [10, 8, 18, 9, 16, 6, 16, 5, 14, 3, 14, 6, 10, 5, 8]
finalCredDigits = finalCredDigits.map(number => {
if (number > 9) {
return number - 9
}
return number
})
console.log(finalCredDigits)

problem in comparing element of two array and if element matches then modifies another array value with some condition

I want to compare x and z and if element in x is present in z then push element of y in temp else push 0 in temp at the end length of z and temp should be equal.
below is my code ---
var x=[00,03,06,21]
var y=[79,11,18,14]
var temp=[]
var z=[00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
for(var i=0;i<z.length;i++){
for(j=0;j<x.length;j++){
if(z[i]==x[j]){
// alert("hello")
temp.push(y[j])
}
}
if(z[i]!=x[j]){
temp.push(0)
}
}
console.log(temp)
console.log(z)
i getting the output as -
//temp (29) [79, 0, 0, 0, 11, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0]
//z (25) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
expected output --
//temp (25) [79, 0, 0, 11, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0]
//z (25) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24]
Once you found a common value, you need to continue the outer loop. If not found, push after finishing the inner loop.
BTW, do not forget to declare all variables.
var x = [0, 3, 6, 21],
y = [79, 11, 18, 14],
temp = [],
z = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24];
outer: for (let i = 0; i < z.length; i++) {
for (let j = 0; j < x.length; j++) {
if (z[i] === x[j]) {
temp.push(y[j]);
continue outer;
}
}
temp.push(0);
}
console.log(temp);
console.log(z);
A version without a label.
var x = [0, 3, 6, 21],
y = [79, 11, 18, 14],
temp = [],
z = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24];
for (let i = 0; i < z.length; i++) {
let value = 0;
for (let j = 0; j < x.length; j++) {
if (z[i] === x[j]) {
value = y[j];
break;
}
}
temp.push(value);
}
console.log(temp);
console.log(z);
Finally, a shorter approach with an object for the replacement values.
var x = [0, 3, 6, 21],
y = [79, 11, 18, 14],
z = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
values = x.reduce((r, k, i) => (r[k] = y[i], r), {}),
temp = z.map(k => values[k] || 0);
console.log(temp);
console.log(z);

different results between javascript and python when using bitwise operators

I am trying to translate some js code into python and having trouble converting bitwise operators. I already introduced ctypes.c_int in python but the results still do not match. For the >>> in js I used as suggested here.
A minimal example of my (not working) code:
Javascript:
let R = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]
let K = [3614090360, 3905402710, 606105819, 3250441966, 4118548399, 1200080426, 2821735955, 4249261313, 1770035416, 2336552879, 4294925233, 2304563134, 1804603682, 4254626195, 2792965006, 1236535329, 4129170786, 3225465664, 643717713, 3921069994, 3593408605, 38016083, 3634488961, 3889429448, 568446438, 3275163606, 4107603335, 1163531501, 2850285829, 4243563512, 1735328473, 2368359562, 4294588738, 2272392833, 1839030562, 4259657740, 2763975236, 1272893353, 4139469664, 3200236656, 681279174, 3936430074, 3572445317, 76029189, 3654602809, 3873151461, 530742520, 3299628645, 4096336452, 1126891415, 2878612391, 4237533241, 1700485571, 2399980690, 4293915773, 2240044497, 1873313359, 4264355552, 2734768916, 1309151649, 4149444226, 3174756917, 718787259, 3951481745]
let blocks = [1819043144, 1919899503, 8414316, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0];
let h0 = 1732584193, h1 = 4023233417, h2 = 2562383102, h3 = 271733878, length = blocks.length;
for (let i = 0; length > i; i += 16) {
var f, g, tmp, x, y, a = h0, b = h1, c = h2, d = h3;
for (let j = 0; 64 > j; ++j) {
if (16 > j) {
f = d ^ b & (c ^ d);
g = j;
tmp = d;
d = c;
c = b;
x = a + f + K[j] + blocks[i + g];
y = R[j];
b += x << y | x >>> 32 - y;
a = tmp;
}
}
}
Python:
import ctypes
R = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]
K = [3614090360, 3905402710, 606105819, 3250441966, 4118548399, 1200080426, 2821735955, 4249261313, 1770035416, 2336552879, 4294925233, 2304563134, 1804603682, 4254626195, 2792965006, 1236535329, 4129170786, 3225465664, 643717713, 3921069994, 3593408605, 38016083, 3634488961, 3889429448, 568446438, 3275163606, 4107603335, 1163531501, 2850285829, 4243563512, 1735328473, 2368359562, 4294588738, 2272392833, 1839030562, 4259657740, 2763975236, 1272893353, 4139469664, 3200236656, 681279174, 3936430074, 3572445317, 76029189, 3654602809, 3873151461, 530742520, 3299628645, 4096336452, 1126891415, 2878612391, 4237533241, 1700485571, 2399980690, 4293915773, 2240044497, 1873313359, 4264355552, 2734768916, 1309151649, 4149444226, 3174756917, 718787259, 3951481745]
blocks = [1819043144, 1919899503, 8414316, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0]
h0 = 1732584193
h1 = 4023233417
h2 = 2562383102
h3 = 271733878
length = len(blocks)
i = 0
while length > i:
a = h0
b = h1
c = h2
d = h3
j = 0
while 64 > j:
if 16 > j:
f = ctypes.c_int(c ^ d & (b ^ c)).value
g = j
tmp = d
d = c
c = b
x = a + f + K[j] + blocks[i + g]
y = R[j]
b += ctypes.c_int(x << y | ((x & 0xffffffff) >> 32 - y)).value # was >>>
a = tmp
j += 1
i += 16
print('a', a)
print('b', b)
js returns 7727443648 and python returns 4225785507 for a
Your values are outside JS limits :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
The numbers -2147483648 and 2147483647 are the minimum and the maximum integers representable through a 32bit signed number.

Restructure one dimensional array into variable length two dimension array

I am trying to convert a one dimensional array into a two dimensional array where the length of the rows are different depending on the maximum number of columns which is supplied as an argument. I basically want to include every nth index till the end of array is reached or max number of columns are done whatever happens earlier. So for the following input:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20]
columns = 7
I want the following output:
[1, 4, 7, 10, 13, 16, 19]
[2, 5, 8, 11, 14, 17, 20]
[3, 6, 9, 12, 15, 18]
I have tried the following code but it only works for few values:
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
function getOutput(arr, columns) {
var rows = Math.ceil(arr.length / columns);
var res = [];
for (var i = 0; i < rows; i++) {
tmp = [];
for (var j = 0, index = i; j < columns; j++) {
if (index < arr.length)
tmp.push(arr[index]);
index += rows;
}
res.push(tmp);
}
return res;
}
console.log(getOutput(array, 7))
JSFiddle available here: https://jsfiddle.net/varunsharma38/fwfz4veo/4/
I do not understand what I am missing. Although, I would not mind using some libraries, I would prefer a vanilla JS based solution.
Thanks in advance!!
Here is another solution using array#reduce.
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
columns = 7,
result = arr.reduce((r,v,i,a) => {
var index = i% (Math.ceil(a.length/columns));
r[index] = r[index] || [];
r[index].push(v);
return r;
},[]);
console.log(result);

Concatenate Arrays -how to concat two dimensional arrays in javascript?

I have the following two arrays in JavaScript:
var array1=[[6, 12, 18],[7, 13, 19],[6, 12, 18],[7, 13, 19],[6, 12, 18],[6, 12, 18]];
var array2=[0,1,2,3,4,5];
How to concat the above arrays such that each element of array2 gets inserted at the end of each array1.
The result should be such that
var merge array=[[0, 6, 12, 18],[1, 7, 13, 19],[2, 6, 12, 18],[3, 7, 13, 19],[4, 6, 12, 18],[5, 6, 12, 18]]
var array1=[[6, 12, 18],[7, 13, 19],[6, 12, 18],[7, 13, 19],[6, 12, 18],[6, 12, 18]];
var array2=[0,1,2,3,4,5];
var merged_array = array1.map(function (value, index) {
value.unshift(array2[index]);
return value;
});
alert(JSON.stringify(merged_array));
var array1=[[6, 12, 18],[7, 13, 19],[6, 12, 18],[7, 13, 19],[6, 12, 18],[6, 12, 18]];
var array2=[0,1,2,3,4,5];
$.each(array1,function(index,val){
val.unshift(array2[index]);
});
Something like this should do it. Note this depends on your arrays being of the same length.
for (var i = 0; i < array2.length; i++)
{
array1[i].unshift(array2[i]);
}

Categories