Creating a Projectile class to calculate height horizontal distance and landing in JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript class, Projectile, which takes in 3 arguments when initialized −

  • starting height (0 ≤ h0 < 200)
  • starting velocity (0 < v0 < 200)
  • angle of the projectile when it is released (0° < a < 90°, measured in degrees).

We need to write the following method for the Projectile class.

  • A horiz method, which also takes in an argument t and calculates the horizontal distance that the projectile has traveled. [takes in a double, returns a double]

Example

The code for this class will be −

 Live Demo

class Projectile{
   constructor(h, u, ang){
      this.h = h;
      this.u = u;
      this.ang = ang;
   };
};
Projectile.prototype.horiz = function(t){
   const dist = 2 * Math.cos(this.ang) * t;
   return dist;
};
const p = new Projectile(5, 2, 45);
const horizontal = p.horiz(.2);
console.log(horizontal);

Output

And the output will be −

0.2101287955270919
raja
Published on 20-Apr-2021 07:07:53
Advertisements