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

162
Visualizações
Update variable values each time variable is called

In the function below I'm concatenating an array of properties to an existing array. What I'm trying to do is each time the concat occurs, it retrieves rankings and when it does the values within rankings are updated with new values returned through generateRankingValue. The problem I'm having is that the rankings value doesn't change when called after the first time. How can I fix this? Thank you!

var generateRankingValue = (min, max) => {
  return Math.round(Math.random() * (max - min) + min);
}

var rankings = [
  {
    trait_type: "Level",
    value: 1,
    max_value: 99,
  },
  {
    trait_type: "Strength",
    value: generateRankingValue(1, 5),
    max_value: 40,
  },
  {
    trait_type: "Stamina",
    value: generateRankingValue(1, 5),
    max_value: 30,
  },
  {
    trait_type: "Attack",
    value: generateRankingValue(1, 3),
    max_value: 50,
  },
  {
    trait_type: "Defense",
    value: generateRankingValue(1, 3),
    max_value: 50,
  },
  {
    display_type: "boost_number",
    trait_type: "Cubic Power",
    value: generateRankingValue(1, 10),
    max_value: 100,
  },
  {
    display_type: "boost_percentage",
    trait_type: "Stamina Increase",
    value: generateRankingValue(1, 10),
    max_value: 50,
  },
];

const addMetadata = (_dna, _edition) => {
  let dateTime = Date.now();
  let tempMetadata = {
    dna: sha1(_dna.join("")),
    name: `#${_edition}`,
    description: description,
    image: `${baseUri}/${_edition}.png`,
    edition: _edition,
    date: dateTime,
    ...extraMetadata,
    attributes: attributesList.concat(rankings), // Concat Properties and Stats Metadata
    compiler: "CCompiler Engine",
  };
  metadataList.push(tempMetadata);
  attributesList = [];
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue with your code is that your variable is defined globally so it is executed at start. What you want is to generate it again everytime you access it. Put it in a function:

var generateRankingValue = (min, max) => {
  return Math.round(Math.random() * (max - min) + min);
}

function GetRandomRankings(){
  var rankings = [
    {
      trait_type: "Level",
      value: 1,
      max_value: 99,
    },
    {
      trait_type: "Strength",
      value: generateRankingValue(1, 5),
      max_value: 40,
    },
    {
      trait_type: "Stamina",
      value: generateRankingValue(1, 5),
      max_value: 30,
    },
    {
      trait_type: "Attack",
      value: generateRankingValue(1, 3),
      max_value: 50,
    },
    {
      trait_type: "Defense",
      value: generateRankingValue(1, 3),
      max_value: 50,
    },
    {
      display_type: "boost_number",
      trait_type: "Cubic Power",
      value: generateRankingValue(1, 10),
      max_value: 100,
    },
    {
      display_type: "boost_percentage",
      trait_type: "Stamina Increase",
      value: generateRankingValue(1, 10),
      max_value: 50,
    },
  ];

  return rankings;
}

const addMetadata = (_dna, _edition) => {
  let dateTime = Date.now();
  let tempMetadata = {
    dna: sha1(_dna.join("")),
    name: `#${_edition}`,
    description: description,
    image: `${baseUri}/${_edition}.png`,
    edition: _edition,
    date: dateTime,
    ...extraMetadata,
    attributes: attributesList.concat(GetRandomRankings()), // Concat Properties and Stats Metadata
    compiler: "CCompiler Engine",
  };
  metadataList.push(tempMetadata);
  attributesList = [];
};
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