Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Take a list of possible combinations and solutions, and set another variable based on which one is selected

I may have worded this strangely idk

I’m making a web app to track kill effects in a game that I play with some friends from school. In the game there are multiple types of "assassins" with different effects sometimes happening on kill. This sometimes gets confusing to keep track of so I’m trying to make something where you can input both types and it will give a result of the corresponding kill effects.
I’ve tried getting results for just one pair as a proof of concept but there are issues with the sheer amount (144 combinations!!!!) and making a separate function for each is obviously not the way to go.
is there a way I can make a list of combinations (ex: a = "Classic", v = "Poison", outcome = "Victim is eliminated, attacker receives 2 day death countdown") then using this list, take attacker and victim classes from variables and set a 3rd variable as whatever the outcome of that pair is?

The code used when I made a single test function was

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


function checkSelection(attackerClass, victimClass) {
    // Check if the selectedAttacker is 'Classic' and the selectedVictim is 'Oxford'
    if (attackerClass === 'Classic' && victimClass === 'Oxford') {
      // Get the result element
      const result = document.querySelector('#outcome');
  
   
      result.textContent = 'No special effect';
    }
  }
  
  let attackerSel = document.querySelector('#attackerSel')
  let victimSel = document.querySelector('#victimSel')
  attackerSel.addEventListener('change', function(){
    var attacker = this.value;
    return attacker;
  })
  console.log(attacker)
  
  setInterval(checkSelection(attacker, victim), 5)

I think I’ve made a mistake with variable scope here, but I don’t think it matters since I’m not going to make 144 different versions

>Solution :

What about using the attacker and victim values to build a keys in an object that’ll give you the result you need? e.g.:

const outcomeMap = {
  'Classic-Oxford': 'No special effect',
  'Classic-Poison': 'Victim is eliminated, attacker receives 2 day death countdown',
  'Piano-Oxford': 'The royal musical society takes out a bounty on attacker',
  'Piano-Poison': 'Victim is poisoned, 2 day death countdown',
};

const attackerSel = document.querySelector('#attackerSel');
const victimSel = document.querySelector('#victimSel');
const outcomeEle = document.querySelector('#outcome');

function checkSelection() {
  const attackerClass = attackerSel.value;
  const victimClass = victimSel.value;
  outcomeEle.textContent = outcomeMap[attackerClass + '-' + victimClass] || 'Not found';
}

attackerSel.addEventListener('change', checkSelection);
victimSel.addEventListener('change', checkSelection);
<form>
  <select id="attackerSel">
    <option value=""></option>
    <option value="Classic">Classic</option>
    <option value="Piano">Piano</option>
  </select>
  <select id="victimSel">
    <option value=""></option>
    <option value="Oxford">Oxford</option>
    <option value="Poison">Poison</option>
  </select>
</form>

<div id="outcome"></div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading