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
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
CodePudding user response:
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>