Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
Dynamic variable declaration. Is this even the right method?

A little new to JS so be gentle :)

I'm trying to create a program that holds 5000+ boolean values that dynamically change based on other vars.

const chars = "abcdefghijklmnopqrstuvwxyz0";
const charsC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0"
const maxNum = 48;
const maxTile = 6;
var tile1, tile2, tile3, tile4, tile5, tile6
// test vars
var tile4 = "A27"
var t4a27 = false

// this snippet will be in an interval loop
for (let i = 1; i <= maxTile; ++i) {
    for (let n = 0; n < chars.length; ++n) {
        for (let j = 1; j <= maxNum; ++j) {
            // this obviously doesnt work
            var t[i][`${chars[n]}`][j];
            // ^ ^ ^
            if (tile[i] == `${charsC[n]}${j}`) {
                t[i][h][j] = true;
                console.log(t4a27)
            } else {
                t[i][h][j] = false;
            }
        }
    }   
}

For clarification a better word than "tile" for the vars could be "sprite" rather because its a point on the sprite.

The basic concept is the tile vars are designed to output their current position as a string value e.g. "A27". Then this loop will take that information and scan each tile subset to be true/false. So if the sprite lower right quadrant is inside "A27" the output would be t4a27 = true

In practice I can do this with just a lot of code (over 20,000 lines) but I figured their has to be an easier way that requires far less code.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This is probably not the right approach for your problem.

If you really need to store this amount of variables, it is probably best to put them in an object like so:

var tiles = {}

var tileName = 'abc'

// Dynamic setting:
tile[tileName] = true

// Dynamic reading:
console.log(tile[tileName])

I am wondering if you really want to store 5000 variables or if there is another way to calculate them at the time you need time, but that requires a bit more knowledge of the problem.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Javascript doesn't have this kind of ability to reflect local variables.
What you can do is attach all those variables to a global object, and proceed with: Object.keys(your_object) and your_object[key_name_here] = ...

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think you should use a 2-dim array for this. Or use a regular array of booleans with the appropriate size and do the index-magic yourself.

As you said, you are running on coordinates. A-27 is the same as row(1)+field(27) -- considering A is 1

If your field is 500x100, you create an Array as such: let gamefield = Array(500*100);

Warning: I have not tested this for syntax errors, but you should get the idea.

let gamefield = Array(500*100);
// optional gamefield.fill(true); 

let row = idx => idx * 500;
let posIdx = (r, c) => row(r) + c;

// there is a sprite with a tiles property that returns 
// 4 index positions for the sprite's quadrants as [r,c]
let quadrants = sprite.tiles.reportPositions()

// filter the quadrants where the gamefield at r,c is true
// this might also be a good case for some() instead of filter()
let collisions = quadrants.filter(pos => return gamefield[posIdx(...pos)]);

// if there is any of these, you can kill the sprite.
if(collisions.length > 0) sprite.kill();

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda