Home > Blockchain >  Update variable values each time variable is called
Update variable values each time variable is called

Time:10-16

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 = [];
};

CodePudding user response:

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 = [];
};
  • Related