Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@ const Point = require("./point");

class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
if(typeof(point1)!="object" && point1 !=undefined || typeof(point2)!="object" && point2 !=undefined){
throw new Error('argument must be type point')
}
if (typeof(n)!="number"&& n !=undefined|| typeof(slope)!="number" && slope !=undefined){
throw new Error('argument must be type number')
}
this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
}

calculateSlope = () => {
if( this.point1.x==this.point1.y && this.point1.y==0 || this.point2.x==this.point2.y && this.point2.y==0){
throw new Error("missing data")
}
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
}

calculateNOfLineFunction = () => {
if(this.slope==undefined || this.point1==undefined){
throw new Error("missing data")
}
this.n = this.point1.y - this.slope * this.point1.x
}

Expand All @@ -26,14 +38,20 @@ class Line {


getPointByX(x) {
if(typeof(x)!="number"){
throw new Error('argument must be type number')
}
let y = this.slope * x + this.n
return new Point({ x, y })
}

getPointByY(y) {
if(typeof(y)!="number"){
throw new Error('argument must be type number')
}
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
}

module.exports = Line
module.exports = Line
11 changes: 10 additions & 1 deletion modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
class Point {
constructor({x=0, y=0}={}) {
if(typeof(x)!="number" || typeof(y)!="number"){
throw new Error('argument must be type number')
}
this.x = x;
this.y = y;
}
moveVertical(value) {
if(typeof(value)!="number"){
throw new Error('argument must be type number')
}
this.y += value;
}
moveHorizontal(value) {
if(typeof(value)!="number"){
throw new Error('argument must be type number')
}
this.x += value;
}
}

module.exports = Point
module.exports = Point
19 changes: 18 additions & 1 deletion modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
const Line = require('./ecs6-class/line')
const Point = require('./ecs6-class/point')

const calculateDistance = (point1, point2) => {
if(!point1 || !point2){
throw new Error('missing data')
}
if(typeof(point1)!= 'object' |typeof(point2)!='object'){
throw new Error('argument must by type point')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write a test that gets an array

}

let distanceX = (point2.x - point1.x) ** 2;
let distanceY = (point2.y - point2.y) ** 2;
let distanceY = (point2.y - point1.y) ** 2;
const distance = Math.sqrt(distanceX + distanceY);
return distance;
}

const calculateJunctionPoint = (line1, line2) => {
if(line1==undefined || line2==undefined){
throw new Error("missing data")
}
if (line1.slope === line2.slope) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if the slope of line1 or line2 are not defined?

if (line1.n === line2.n) {
return true
Expand All @@ -24,6 +35,9 @@ const calculateJunctionPoint = (line1, line2) => {
}

const isPointOnLine = (line, point) => {
if(line==undefined || point==undefined){
throw new Error("missing data")
}
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when line.slope or line.n are undefined?

if (line.slope === proxyLine.slope) {
Expand All @@ -40,3 +54,6 @@ module.exports = {
calculateJunctionPoint,
isPointOnLine
}



3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "practice unit tests in javascript",
"main": "index.js",
"scripts": {
"test": "jest"
"test": "jest",
"test:coverage": "jest --coverage"
},
"dependencies": {
"jest": "^29.7.0"
Expand Down