Git Product home page Git Product logo

oop-exercises's Introduction

JavaScript Objects and Constructors

Person

Given this code:

class Person {
  constructor(name, email, phone) {
      this.name = name;
      this.email = email;
      this.phone = phone;
  }
  greet(otherPerson) {
    console.log('Hello ' + otherPerson.name + ', I am ' + this.name + '!');
  }
}

Write code to

  1. Instantiate an instance object of Person with name of 'Sonny', email of '[email protected]', and phone of '483-485-4948', store it in the variable sonny.
  2. Instantiate another person with the name of 'Jordan', email of '[email protected]', and phone of '495-586-3456', store it in the variable 'jordan'.
  3. Have sonny greet jordan using the greet method.
  4. Have jordan greet sonny using the greet method.
  5. Write a statement to print the contact info (email and phone) of Sonny.
  6. Write another statement to print the contact info of Jordan.

Card

Create a class Card. A card object will have 2 properties:

point - the point value of the card: a number between 1 and 13. suit - the suit of the card: one of diamonds, clubs, hearts and spades.

A card will be created thus:

> let myCard = new Card(5, 'diamonds')

And you can access the individual properties like:

> myCard.point
5
> myCard.suit
'diamonds'

getImageUrl()

Add a getImageUrl method to card objects by adding it as a member method to your Card class. The method should return the URL path to the png image file for the card.

> myCard.getImageUrl()
'images/5_of_diamonds.png'

Hand constructor

A hand is simply represented as a collection of cards, but it would also be convenient for a hand to be able to return it's point value. We would like to be able to do this with a Hand constructor:

> var myHand = new Hand()
> myHand.addCard(new Card(5, 'diamonds'))
> myHand.addCard(new Card(13, 'spades'))
> myHand.getPoints()
15

Implement a Hand class that will allow the code above to work. Hint: you will want to store as a property of a hand object - an array of card objects.

Deck constructor

A deck is also represented as a collection of cards, but it would also be convenient for it to be able to shuffle itself, and be asked to draw a card. We would like to be able to do this with a Deck constructor:

> var myDeck = new Deck()
> myDeck.draw()
Card {point: 6, suit: "clubs"}
> myDeck.draw()
Card {point: 1, suit: "spades"}
> myDeck.shuffle()
> myDeck.numCardsLeft()
50

Implement a Deck class that will allow for the above code to work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.