Giter Club home page Giter Club logo

classes_assignment's Introduction

Class Exercises

  • Create a Human class that takes in a name and age.
  • Add the function ageOneYear that ages the human.
  • Add the function eating, that logs "mmm, mmm, mmm, I'm love'n it".
  • Create an instance of the Human class.
    • console log your humans age
    • call ageOneYear on your human
    • console log their age again.
    • call eating on your human.
class Human {
  constructor(name, age){
    this.name = name;
    this.age = age;
  }

  ageOneYear(){
  this.age++
  return `One year has passed now I'm ${this.age}`
  }

  eating(){
    return `Just ate. mmm, mmm, mmm, I'm love'n it.`
  }
}

let thomas = new Human('Thomas', 30);
console.log(thomas.ageOneYear());

Write a class Vector that represents a vector in two-dimensional space. It takes two number arguments: x and y parameters, which it should be saved to properties of the same name.

Give the Vector two methods, plus and minus, that take another vector as an argument and returns a new vector that has the sum or difference of the two vectors’ (the one in this and the parameter) x and y values.

Add a method getLength to the prototype that computes the length of the vector ; that is, the distance of the point (x, y) from the origin (0, 0).(a^2 + b^2 = c^2)

var v1 = new Vector(1, 2)
var v2 = new Vector(2, 3)
console.log(v1.plus(v2));
// => Vector {x: 3, y: 5}
console.log(v1.minus(v2));
// => Vector {x: -1, y: -1}

var v3 = new Vector(3, 4)
console.log(v3.getLength());
// => 5
class Vector{
  constructor(x,y){
  this.x = x;
  this.y = y;
}

plus(vec){
  let sum1 = this.x + vec.x;
  let sum2 = this.y + vec.y;
  return `x:${sum1}, y:${sum2}`
}

minus(vec){
  let sum1 = this.x - vec.x;
  let sum2 = this.y - this.y;
  return `x:${sum1}, y:${sum2}`
}

getLength(){
  return Math.hypot(this.x, this.y)
}
}


let v1 = new Vector(1,2)
let v2 = new Vector(2,3)
let v3 = new Vector(3,4)
console.log(v1.plus(v2));
console.log(v1.minus(v2));
console.log(v3.getLength());

classes_assignment's People

Contributors

coreyladovsky avatar jevit01 avatar

Watchers

James Cloos 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.