- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Creating a Projectile class to calculate height horizontal distance and landing in JavaScript
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 −
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
Advertisements