const scale = 12;
const lifeSpans = [100, 150, 200, 250, 300, 350, 500];

var ghostImg = new Image();
ghostImg.src = "images/ghost.png";

function Ghost(options){
  this.x = getRandomPosition(options.map);
  this.y = getRandomPosition(options.map);
  this.lifeSpan = lifeSpans[Math.floor(Math.random() * lifeSpans.length)];
  this.ctx = options.ctx;

  this.opacity = 0.10;
  this.width = options.width || 15;
  this.height = options.height || 15;
  this.image = options.image || ghostImg;
}

Ghost.prototype.chasePlayer = function(playerX, playerY){
  let newIncrements = this.distanceToPlayer(playerX, playerY);
  let newX = newIncrements[0] / scale;
  let newY = newIncrements[1] / scale;

  this.x < playerX ? (this.x += newX) : (this.x -= newX);
  this.y < playerY ? (this.y += newY) : (this.y -= newY);
}

Ghost.prototype.distanceToPlayer = function(playerX, playerY){
    let distX = Math.abs(this.x - playerX);
    let distY = Math.abs(this.y - playerY);

    let alpha = Math.atan(distY / distX);

    let newX = Math.cos(alpha);
    let newY = Math.sin(alpha);

    return [newX, newY];
}

Ghost.prototype.fadeEffect = function(){
  let lastGlobalAlpha = this.ctx.globalAlpha;
  this.ctx.globalAlpha = this.opacity;
  this.draw();
  this.ctx.globalAlpha = lastGlobalAlpha;
}

Ghost.prototype.draw = function(){
  this.ctx.drawImage(
    this.image,
    0,
    0,
    this.width,
    this.height,
    this.x * scale - 2,
    this.y * scale - 2,
    this.width,
    this.height);
}

Ghost.prototype.update = function(player, map){
  this.chasePlayer(player.x, player.y, map);

  this.lifeSpan++;
  if (this.lifeSpan % (scale * scale) === 0) {

    this.opacity = 0.5;
    this.fadeEffect();

    this.opacity = 0.25;
    this.fadeEffect();

    this.randomRelocate(map);
    this.opacity = 0.05;
  } else {
    this.opacity += 0.05;
    this.fadeEffect();
    this.collisionCheck(player, map);
  }
}

Ghost.prototype.randomRelocate = function(map) {
  this.x = getRandomPosition(map);
  this.y = getRandomPosition(map);
}

Ghost.prototype.collisionCheck = function(player, map){
  let x = Math.floor(this.x);
  let y = Math.floor(this.y);
  let playerX = Math.floor(player.x);
  let playerY = Math.floor(player.y);

  if (x === playerX && y === playerY && player.pillCount > 0) {
    player.pillCount -= 1;
  }
}

function getRandomPosition(map) {
  return Math.floor(Math.random() * (map.height)) + 1;
}

module.exports = Ghost;

@@ -71,11 +71,13 @@
 %5D;%0A%0A
-var
+const
  gho
@@ -607,23 +607,23 @@
 playerY)
-;%0A  let
+,%0A     
  newX = 
@@ -646,23 +646,23 @@
  / scale
-;%0A  let
+,%0A     
  newY = 
@@ -912,25 +912,25 @@
 playerX)
-;
+,
 %0A    
-let
+   
  distY =
@@ -956,26 +956,25 @@
 playerY)
-;%0A%0A
+,%0A  
     
-let
+ 
  alpha =
@@ -1002,18 +1002,17 @@
 stX)
-;%0A%0A
+,%0A   
     
-let
  new
@@ -1034,17 +1034,17 @@
 pha)
-;
+,
 %0A    
-let
+   
  new
@@ -1109,26 +1109,23 @@
 ototype.
-fadeEffect
+addFade
  = funct
@@ -1574,17 +1574,16 @@
 , map);%0A
-%0A
   this.l
@@ -1593,16 +1593,105 @@
 Span++;%0A
+  this.fadeEffects(player, map);%0A%7D%0A%0AGhost.prototype.fadeEffects = function(player, map)%7B%0A
   if (th
@@ -1729,17 +1729,16 @@
 == 0) %7B%0A
-%0A
     this
@@ -1758,34 +1758,31 @@
 5;%0A    this.
-fadeEffect
+addFade
 ();%0A%0A    thi
@@ -1804,34 +1804,31 @@
 5;%0A    this.
-fadeEffect
+addFade
 ();%0A%0A    thi
@@ -1921,26 +1921,23 @@
    this.
-fadeEffect
+addFade
 ();%0A    
@@ -2183,23 +2183,23 @@
 (this.x)
-;%0A  let
+,%0A     
  y = Mat
@@ -2213,23 +2213,23 @@
 (this.y)
-;%0A  let
+,%0A     
  playerX
@@ -2255,15 +2255,15 @@
 r.x)
-;%0A  let
+,%0A     
  pla

Refactor to remove comments / unused methods