JustinLogue / project1

0 stars 0 forks source link

Redundant Functions #1

Open aidanbell opened 4 years ago

aidanbell commented 4 years ago

https://github.com/JustinLogue/project1/blob/acd8c29b4b337027dbf15db2e89a5c44168b16d3/script.js#L662-L679 Your event listener functions are almost identical. Remember that you can define/declare a function, and pass it into the event listener as the call back. This could help you cut down on a lot of lines of code.

For example:

var spellsReady = document.querySelector('.spells'); 
var movePicked = document.querySelector('.movement'); 

function castSpell(evt) {
     if (player.mp >= spell[evt.target.className].mpCost){ 
          spell[evt.target.className].Cast(); 
          foeSpell(); 
          spellsReady.style.display = 'none'; 
          movePicked.style.display = 'flex'; 
     } else {
          alert("You don't have enough mana to cast this spell!")
     } 
 }); 

sh1.addEventListener('click', castSpell);
jtuppy commented 4 years ago

This occurs a lot in the app. We want to try to keep our code as "DRY" as possible, which means that we want to encapsulate a lot of the same functionality into reusable functions. This will really reduce the overall amount of code and make it easier to manage.