Giter Club home page Giter Club logo

constructor_assignment's Introduction

Constructor Exercises

  • Create a Human constructor function that takes in a name and age.
  • Add a prototype function ageOneYear that ages the humans age.
  • Add a prototype 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.
function Human(name,age){
  this.name = name;
  this.age = age;
}
let thomas = new Human("Thomas",30)
console.log(thomas);

Human.prototype.ageOneYear = function(){
  this.age++;
  return `One year has passed now I'm ${this.age}`
}
console.log(thomas.ageOneYear());

Human.prototype.eating = function(){
  return "Just ate. Mmm, mmm, mmm, I'm love'n it."
}
console.log(thomas.eating());

Write a constructor 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 prototype 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
function Vector(x, y){
  this.x = x;
  this.y = y;
}

let v1 = new Vector(2,5)
let v2 = new Vector(3,7)

console.log(v1);
console.log(v2);

Vector.prototype.plus = function(vec){
  let sum1 = this.x + vec.x
  let sum2 = this.y + vec.y
  return `x: ${sum1}, y: ${sum2}`
};
console.log(v1.plus(v2));

Vector.prototype.minus = function(vec){
  let sum1 = this.x - vec.x
  let sum2 = this.y - vec.y
  return `x: ${sum1}, y: ${sum2}`
};
console.log(v1.minus(v2));

Vector.prototype.getLength = function(){
  return Math.hypot(this.x, this.y)
}
let v3 = new Vector(3, 4)
console.log(v3);
console.log(v3.getLength());

constructor_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.