Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!