GithubHelp home page GithubHelp logo

javascript-classes's Introduction

JavaScript Classes

Let's put our knowledge of classes to practice! In this assignment, you will create JavaScript objects that model real life objects using classes as a blueprint.

Instructions:

  • Use node to run the JavaScript files created in the exercises below.

1. Model a Rectangle

  • create a file named Rectangle.js

  • in it, create a class named Rectangle, with the following properties in the constructor:

    • breadth
    • length
  • define a method named area() to return the area of the rectangle

    • formula: area = length * breadth
  • instantiate the following rectangles one by one and verify that area() returns the expected values as stated below

    • you can use console.log to log the results and verify them
breadth length area() perimeter()
2 4 8 12
5 10 50 30

Repeat these steps for perimeter()

  • formula: perimeter = 2 * length + 2 * breadth

2. Model a Circle

  • create a file named Circle.js
  • in it, create a class named Circle, with the following property in the constructor:
    • radius
  • define a method named area() to return the area of the circle
    • formula: area = Math.PI * r ** 2
    • hint: search "how to round a number to 1 decimal place" on google to find out how to generate the output as required
  • instantiate the following circles one by one and verify that area() returns the expected values as stated below
radius area() perimeter()
1 3.1 6.3
2 12.6 12.6
3 28.3 18.8

Repeat these steps for perimeter()

  • formula: perimeter = 2 * Math.PI * r

3. Bonus: Model a SimpleCalculator

  • For this bonus exercise, don't use assertEquals. Instead, console.log to see the output of your methods and manually check whether it matches your expected values
  • create a file named SimpleCalculator
  • in it, create a class named SimpleCalculator, with the following property in the constructor:
    • value (this will be the starting value). If no argument is supplied during instantiation, value should default to 0
  • Implement (i.e. write code for) the following methods.
    • calculator.add(n)
    • calculator.subtract(n)
    • calculator.multiply(n)
    • calculator.divide(n)
  • Implementation logic for each method:
    • take in one argument (n)
    • use n to update the value of this.value
    • return this so that we can chain methods (example: calculator.add(12).multiply(2).divide(2))

Copyright (c) 2018 ThoughtWorks. For personal use and training purposes only; not to be copied or distributed without further approval.

javascript-classes's People

Contributors

davidtan-tw avatar davified avatar songguoqiang avatar spinningarrow avatar

Watchers

 avatar

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.