Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

293
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda