I've got a 2 dimensional array (I'll call it myArray). myArray is full of 10 child arrays, each with 10 "-"s.
function mapInit () {
max_size = 10;
board = [];
map_width = Math.floor((Math.random() * max_size + max_size) / 2);
map_height = Math.floor((Math.random() * max_size + max_size) / 2);
boardsize = { x : map_width, y : map_height }
}
function generateBoard () {
var columns = [];
for (var i = 0; i < map_height; i++) {
columns.push("---");
};
for (var i = 0; i < map_width; i++) {
board.push(columns);
};
}
When I select myArray[x][y], it returns the value of the single object in that array: "-". This makes sense because I asked for that individual value.
When I set myArray[x][y] = 1, it sets all [y] in the second-level arrays to 1. It should set the individual value in that specific child array to 1, because the individual value is what was just returned when I selected myArray[x][y]. What am I doing wrong / not understanding?
What am I doing wrong / not understanding?
You adding a reference to a single array multiple times to another array. Take a look at this simplified example:
var a = [];
var b = [a, a];
a[0] = 42;
console.log(b);
// [[42], [42]]
as you can see, I'm setting a as the first and second element in the b array. There is no reason why this operation should create two copies of a in the process. Both elements reference the same array, which you can easily test with
b[0] === b[1] // true
Two distinct arrays would never be equal to each other ([] === [] returns false).
Solution: Create a copy of the array inside the second loop:
for (var i = 0; i < map_width; i++) {
board.push(columns.slice(0));
}
Related
I have a nested array like this:
[ [5, 10, 5, 15, 5, 10], [10, 15, 50, 200] ]
So basically I have index 0 that also has an array with the values [5,10,5,15,5,10] and index 1 with an array with values [10,15,50,350].
let array = [[],[]];
let x = 0;
let y = 0;
for (let i = 0; i < 10; i++) {
x = ... generate random number ...
y = ... generate random number ...
while (array[0].includes(x,y) {
x = ... generate new random number ...
y = ... generate new random number ...
}
array[0].push(x,y);
}
Is there a way for me to find if for example index 0 in the array already contains the two generated values in the same order they are being pushed?
For example, I'm adding value 5,15 to the array. And the array already contains index 0 with 5 and index 1 with, 15 in that order. So I want to generate a new number if the numbers already exist in that order.
I've tried looking for solutions, but haven't found anything that helps me with what I wanna do.
I recommend making a function that checks for a specific sequence of numbers, this way if you need it to find a X number sequence you're ready.
const max = 100;
let array = [[], []];
let x = 0,
y = 0;
for (let i = 0; i < 1000; i++) {
x = Math.floor(Math.random() * max);
y = Math.floor(Math.random() * max);
while (containsSequence(array[0], [x, y])) {
console.log(`Sequence ${x},${y} already exists.`);
x = Math.floor(Math.random() * max);
y = Math.floor(Math.random() * max);
}
array[0].push(x, y);
}
/**
* #param {Array<Number>} arr
* #param {Array<Number>} sequence */
function containsSequence(arr, sequence) {
if(arr.length === 0) { return false; }
const first = sequence[0];
let index = arr.indexOf(first, 0);
while(index > -1) {
if(sequence.every((v, i) => arr[index + i] === v)) {
return true;
}
index = arr.indexOf(first, index);
if(index == -1) { return false; }
index++;
}
return false;
}
You are putting x and y one after each other in the list, if you connect them to an object or an array, you can iterate over the outer array more easily:
for (let i = 0; i < 10; i++) {
x = ... generate random number ...
y = ... generate random number ...
while (array[0].filter(i=>i.x == x && i.y == y).length) {
x = ... generate new random number ...
y = ... generate new random number ...
}
array[0].push({x,y});
}
it will also make it more simle. Of course,only if you can make this change to the array structure
Assuming that this is just a general structural question as mentioned in the comments, you can adapt your code as below.
for (let i = 0; i < 10; i++) {
int index = 1;
while (index < array[0].length){
if ((array[0][i][index-1].includes(x) && array[0][i][index].includes(y)) {
x = ... generate new random number ...
y = ... generate new random number ...
index = 0;
}
else{
index++;
}
}
array[0].push(x,y);
}
}
Are there any more efficient ways to create nested objects than nested for approach?
I am trying to hold 25 random numbers (1 - 71), 5 row x 5 column. I will iterate through each item to check if ballPicker() function (not yet implemented) has picked a number that the object has. So I need to keep the information that if a number was found. I could have done it with a 5 x 5 array but iterating that array will not be as efficient as objects.
I have an array starting from 1 to 71 (inclusive, step is 1).
I shuffle the array in createRandomNumberArray() function. So shuffledNumbers array values are not sequential
let shuffledNumbers = Array.from({length: 71}, (v, i) => i + 1);
function createRandomNumberArray() { //Fisher - Yates shuffle algorithm
let randomPosition;
let temp;
for (let i = shuffledNumbers.length - 1; i > 0; i--) {
randomPosition = Math.floor(Math.random() * (i + 1));
temp = shuffledNumbers[i];
shuffledNumbers[i] = shuffledNumbers[randomPosition];
shuffledNumbers[randomPosition] = temp;
}
}
In createBoards() function I am creating an object (board) that holds 5 objects (row) with their indexes as property name. And row has 5 objects (cell) with their indexes as property name again. Cell has only one object that gets its property name from slicedArray(slicedArray is a 5 item slice from shuffledNumbers that holds non duplicated random numbers (range inclusive 1 to inclusive 71)) and its value is always false.
function createBoards() {
let board = {};
for (let i = 0; i < 5; i++) {
let row = {};
let slicedArray = shuffledNumbers.slice((i * 5), ((i + 1) * 5));
for (let j = 0; j < 5; j++) {
let cell = {};
cell[slicedArray[j]] = false;
row[j] = cell;
}
board[i] = row;
}
return board;
}
I want to keep the structure like this:
Board object will have exactly 5 rows and 1 row has exactly 5 items. And in the future I need to iterate those items to find the random value that I had assigned before. I thought it is faster to use objects than arrays. I hope I am correct.
I am trying to create a function that will generate a random number and store it in an array so the first click will send the random number to the index[0] click 2 to index [1] ect. I need to be able to compare the number with the one before (index [4] with index [3].I am sure the answer is right in front of me but i cannot find a solution. Any help would be fantastic
for(i = 0;i < 12;i++) {
var random_number = Math.floor(Math.random() * 12);
var myArray = [];
myArray.push(random_number);
console.log(myArray.length);
document.getElementById("catchme").innerHTML = random_number;
}
});
http://codepen.io/kingnarwal/pen/BzjRjq?editors=1111
var myArray = [];
for(var x = 0, maxValue = 12, random_number; x < 12; x++) {
do {
random_number = Math.floor(Math.random() * maxValue );
} while(random_number == myArray[x - 1]);//Check if the number is the same value
myArray.push(random_number);
}
console.log(myArray);
This does not generate an array with random unique numbers since you're only checking the item before the current item.
To make values unique in whole array:
var myArray = [];
for(var x = 0, maxValue = 12; x < maxValue; x++) {
myArray.splice(Math.floor(Math.random() * myArray.length), 0, x);
}
console.log(myArray);
Above is a bit hackish method since it uses splice with an random index :P
Keep in mind that above method is FAR from random.
A more random method would be:
var myArray = [];
for(var x = 0, x < 12; x++) {
myArray.push(x);
}
shuffle(myArray);
console.log(myArray);
You can use an array shuffle method from here: How to randomize (shuffle) a JavaScript array?
I have ~16.000 items (one item is an array, has 6 values). All items have 5 "attributes", they're numbers. I need to store them, and when I know the attributes, return the item, as fast as possible. I tried 2 kind of methods:
Objects in objects. If a = attribute1, b = attribute2 ... to e, then I can get the item the user's looking for by data1[a][b][c][d][e]. The full data stored in this format: http://jpst.it/z12-
One object, but it has 16k arrays, the object's properties are the given item's attributes. Then myItem = data2[""+a+b+c+d+e]. http://jpst.it/z13p
I ran some tests to see which method is the faster:
var test,
a = 30175,
b = 5,
c = 1,
d = 1,
e = 60,
abcde = 301755360;
console.time("test1");
for (var i = 0; i < 9999999; i++)
test = data1[a][b][c][d][e];
console.timeEnd("test1");
console.time("test2");
for (var i = 0; i < 9999999; i++)
test = data2[abcde];
console.timeEnd("test2");
Results on my PC:
test1 test2
Firefox: 1482.00ms 1341.00ms
Chrome: 665.00ms 424.00ms
Explorer: 12555.17ms 4945,50ms
The second method is always faster. But why? I ran another test to know how many times the JS have to check if the given object's key equals to the needed key:
var x = 0;
function fn(obj, myKey) {
for (var key in obj) {
x++;
if (key == myKey)
return obj[key];
}
}
Method 1: fn(fn(fn(fn(fn(data1, a), b), c), d), e); => x is 352.
Method 2: fn(data2, abcde); => x is 3154.
However, method 2 isn't 10x slower, it's even faster.
This might give you an idea of what's going on. 2nd getter is called 9 times, first getter is called 45 times. Each time it first has to access 30186, then 6, then ... 5 calls for each iteration to get the value.
var x = 0;
var y = 0;
var data1 = {"30186":{"6":{"1":{"1":{"0":[4,1430745862,"3 - 5 ref","4 ref","4 ref",1]}}}}};
var data2 = {"3018663":[4,1430745862,"3 - 5 ref","4 ref","4 ref",1]};
Object.prototype.get = function(i) { x++; console.log(i); return this[i]; }
Object.prototype.get2 = function(i) { y++; return this[i]; }
for (var i = 0; i < 9; i++)
test = data1.get(30186).get(6).get(1).get(1).get(0);
for (var i = 0; i < 9; i++)
test = data2.get2(3018663);
console.log(x, y);
My goal is to make a randomly generated 2D Array in Javascript, that has an X amount of the same one character value while the rest of the values are equal to another character.
In this example, there are 10 rows and 10 columns for the 2D Array. 20 out of the possible 100 values of the Array should be equal to 'Y' (for yes) and the 80 others should be 'N' (for no). I want the 'Y's to be randomly placed all over the Array, and I absolute need exactly 20 of them to be 'Y's and the rest 'N's.
I had a less efficient way before, and I thought to try this approach, where after I define the Array, I make the first X amount of values a 'Y' and then the rest all 'N's. Then I shuffle the array, (using the shuffle from the underscore library) so that the 'Y's are all spread out randomly everywhere.
Is this an efficient way of getting what I need done? Are there any better solutions? I tried making a JSFiddle with my example, but the site appears to be down at the moment.
(I was unable to test my code yet to see if the shuffle worked correctly on my 2D array)
var rows = 10;
var cols = 10;
var elements = 20;
//Define Empty Array
var test = new Array(rows);
for (var k = 0; k < rows; k++)
{
test[k] = Array(cols);
}
var i = 1;
for (var x = 0; x < rows; x++)
{
for (var y = 0; y < cols; y++)
{
if (i <= elements)
{
test[x][y] = "Y";
}
else
{
test[x][y] = "N";
}
}
}
//Shuffle all those values so they're no longer in order
var shuffledTest = _.shuffle(test);
//Print in rows
for (var x = 0; x < rows; x++)
{
console.log(shuffledTest[x]);
}
A very simple solution is to first create an array, fill it with a number of "N"s, insert the "Y"s at random indexes, and then finally splitting it into the 2-dimensional array that you want:
var tmpArr = [], // Temporary 1-dimensional array to hold all values
arr = [], // The final 2-dimensional array
rows = 10,
cols = 10,
elements = 20; // Number of "Y"s
// 1. Fill temporary array with "N"s
for (var i = 0; i < rows * cols - elements; i += 1) {
tmpArr.push("N");
}
// 2. Insert "Y"s at random indexes in the temporary array
for (var i = 0; i < elements; i += 1) {
var index = Math.round(Math.random() * (tmpArr.length + 1));
tmpArr.splice(index, 0, "Y");
}
// 3. Split temporary array into 10 seperate arrays
// and insert them into the final array
for (var i = 0; i < rows; i += 1) {
var row = tmpArr.slice(i * cols, (i + 1) * cols);
arr.push(row);
}
JSBin to illustrate: http://jsbin.com/luyacora/1/edit
You can try this solution, it uses underscores range to create a pair of arrays to use as iterators, though their values don't matter.
Play around with the randomizer function to get an even distribution of 'y's
JSBIN: http://jsbin.com/yaletape/1/
var rows = _.range(0, 10, 0);
var columns = _.range(0, 10, 0);
function randomizer(mult){
return Math.floor((Math.random()*mult)+1);
}
var y_count = 0;
var matrix = _.map(rows, function(){
return _.map(columns, function(v, i){
var value;
var y_allowed = randomizer(3);
var current_y_count = 0;
if(y_count < 20 && current_y_count < y_allowed){
var rand = randomizer(5);
if(rand > 4){
value = 'y';
current_y_count++;
y_count++;
}
}
if(!value){
value = 'n';
}
return value;
});
});
//The above could be simplified to
var matrix = _.range(0,10,0).map(function(){
return _.range(0,10,0).map(function(){
//put the logic code from above here
});
});
Maybe shuflle a 2D array is not the best way. As #Zeb mentioned, here is some code that fill random positions with the 'Y' value. After that, the other positions are filled with 'N'.
http://plnkr.co/edit/avyKfgsgOSdAkRa1WOsk
var arr = [];
var cols = 10;
var rows = 10;
var positions = rows*cols; // 100
var YQty = 10; // only 10 'Y' are needed
// 'Y' values.
for(i = 0; i < YQty; i++)
{
do
{
x = parseInt(Math.random() * cols);
y = parseInt(Math.random() * rows);
filled = false;
if (typeof(arr[x]) == "undefined")
{
arr[x] = [];
}
if (typeof(arr[x][y]) == "undefined")
{
arr[x][y] = 'Y';
filled = true;
}
}
while (!filled);
}
// 'N' values.
for (x = 0; x < cols; x++)
{
if (typeof(arr[x]) == "undefined")
{
arr[x] = [];
}
for (y = 0; y < rows; y++)
{
if (arr[x][y] != 'Y')
{
arr[x][y] = 'N';
}
}
}
Shuffling the multidimensional array is not the best approach. Seeing as any sort is worse than linear time complexity. The easiest solution would be to create your multidimensional array and then set each index value to the char you want the 'rest' of the values to be. Then for 1 -> the number of other char value choose a random index and set that to the char.
Note: If the randomly picked spot has already been changed you need to choose a new one to make sure you have the right amount at the end.