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

294
Vistas
How can I find a specific array assigned to a randomly selected variable?

So I'm trying to make a function for a text RPG that allows people to click a "wander" button and end up in a random location near their current one. I've created several arrays that contain the information for which locations are near others, and I'd like to be able to press a button, generate a new location, save that location as the current location, and generate a new wander result from the new location the next time the button is pressed.

//Starting area locations
    const startingAreaLocations = [Clearing, BigForrest, SmallForrest, Cliffs, Cave, Pond, Start, River, TownEnterance];
    const locationsNearStart = [BigForrest, Cliffs, Cave];
    const locationsNearBigForrest = [Start, Clearing, River];
    const locationsNearCliffs = [Start, Cave, River];
    const locationsNearCave = [Cliffs, Pond, River, Start];
    const locationsNearClearing = [BigForrest, River, Start];
    const locationsNearSmallForrest = [Pond, River, TownEnterance];
    const locationsNearPond = [SmallForrest, Cave, River];
    const locationsNearRiver = [BigForrest, Cliffs, Cave, Clearing, SmallForrest, Pond, TownEnterance];
    const locationsNearTowerEnterance = [SmallForrest, River];

My issue at the moment is that when I generate and load a new location, I don't know how to tell the system which array it should be referencing for the next locations. I tried to name the variables so that I could add the location name variable to a string and come out with the array name, but even when I put it through a JSON.parse(), it reads the string, not the contents of my array.

function wander(location) {
                wanderLocation = location[Math.floor(Math.random()*location.length)];
                console.log(wanderLocation);
                locationsNearCurrentArea = "locationsNear" + wanderLocation.name;
                console.log(locationsNearCurrentArea);
                
                callPannel(wanderLocation.string);
            }

How can I better make a feature that will let me bounce between locations like this?

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

0

I would use an explicit data structure for this instead of a bunch of variables. A Map with the current location as key and nearby locations in an array as the value should suffice

const nearbyLocations = new Map([
//[ key,        [ values, ... ] ],
  [ Start,      [ BigForrest, Cliffs, Cave ] ],
  [ BigForrest, [ Start, Clearing, River ] ],
  [ Cliffs,     [ Start, Cave, River ] ],
  // etc
])

Then you can use something like this to get a random, nearby location

const wander = (currentLocation) => {
  const nearby = nearbyLocations.get(currentLocation)
  return nearby?.[Math.floor(Math.random() * nearby?.length)]
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You should put the data at least in JSON, object, or list. Here is an example using object:

let data = {
  startingAreaLocations: ["Clearing", "BigForrest", "SmallForrest"],
  locationsNearStart: ["BigForrest", "Cliffs", "Cave"],
  locationsNearBigForrest: ["Start", "Clearing", "River"],
};
let newLocation = "River";
let newWander = ["SmallForrest", "BigForrest"];

let dataKey = Object.values(data);
data[Object.keys(data)[Object.keys(data).length - 1]].forEach((e) => {
  if (e === newLocation) {
    data[`locationsNear${e}`] = newWander;
  }
});

console.log(data);

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