Exercises
To test Steve’s understanding, Julia presented him with three coding challenges related to pure functions and side effects. “These exercises will help solidify the concepts,” she explained. “Give them a try and let me know if you have any questions.”
Exercise 1
Steve’s tower defense game calculates damage based on a global difficulty modifier. Refactor this function to make it pure:
public static double difficultyModifier = 1.0; public double CalculateDamage(Tower tower, Enemy enemy) { return tower.BaseDamage * enemy.DamageMultiplier * difficultyModifier; }
Exercise 2
Steve’s game loads enemy data from a file, processes it, and updates the game state. Refactor this function to isolate its side effects:
public void LoadAndProcessEnemyData(string filePath) { string jsonData = File.ReadAllText(filePath); List<Enemy>...