Skip to content

Week 2 for Cohort XI, review session. Mostly covering OOP concepts and how they relate to JS.

Notifications You must be signed in to change notification settings

ogryzek/js_review

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 

Repository files navigation

#JavaScript Review: Review Session 2 This past week, we looked at JavaScript, we looked at NodeJS, and we developed a JukeBox app.

Let's take what we've learned and apply it to what we know about OOP. That is, let's translate our Pacman, Ghost, and Ball classes from Ruby into JavaScript.

Ball Class

Our Ball class should have a ball_type attribute whose value can be either regular or super,

// ball.js
var Ball = function(ballType) {
  this.ballType = ballType === undefined ? "regular" : "super";
};

Ghost Class

Now that we are comfortable (somewhat) with classes in JavaScript, let's implement a Ghost class which sets its color to red, pink, white, or yellow randomly on instantiation. Also, let's use javascript's prototype in our code.

// ghost.js
var Ghost = function() {
  this.color = ["pink", "red", "yellow", "white"][Math.floor(Math.random() * 4)]
}

PacMan Class

Using what we've learned from the previous examples, let's implement a PacMan class that can eat ghosts and balls, and also has some attributes such as points and lives.

Use our iRuby version as an example.

// pacman.js
var Pacman = function() {
  this.extraLives = 2;
  this.points = 0;
  this.superTime = 0;
}

Pacman.prototype = {
  eat: function(obj) {},
  eatBall: function(ball) {
    this.superTime -= 1;
    this.points += ball.ballType === "regular" ? 1000 : 10;
    if (ball.ballType === "super") {
      this.superTime = 10;
    }
  },
  eatGhost: function(ghost) {},
  die: function() {},
  gameOver: function() {}
}

About

Week 2 for Cohort XI, review session. Mostly covering OOP concepts and how they relate to JS.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published