-
Notifications
You must be signed in to change notification settings - Fork 41
add tests #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
add tests #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,37 @@ | ||
|
|
||
|
|
||
| class Point { | ||
| constructor({x=0, y=0}={}) { | ||
|
|
||
| constructor({ x = 0, y = 0 } = {}) { | ||
|
|
||
| if (typeof (x) !== 'number' && typeof (y) !== 'number') | ||
| throw new Error('x and y are not number') | ||
| if (typeof (x) !== 'number') | ||
| throw new Error('x is not number') | ||
| if (typeof (y) !== 'number') | ||
| throw new Error('y is not number') | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
|
|
||
| moveVertical(value) { | ||
|
|
||
| if (!value) | ||
| throw new Error('value is undefined') | ||
| if (typeof value !== 'number') | ||
| throw new Error('value is not number') | ||
| this.y += value; | ||
| } | ||
|
|
||
|
|
||
| moveHorizontal(value) { | ||
| if (!value) | ||
| throw Error('value is undefined') | ||
| if (typeof value !== 'number') | ||
| throw Error('value is not number') | ||
|
|
||
| this.x += value; | ||
| } | ||
| } | ||
|
|
||
| module.exports = Point | ||
| module.exports =Point |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| const Line = require('./ecs6-class/line') | ||
| const Point = require('./ecs6-class/point') | ||
|
|
||
| const calculateDistance = (point1, point2) => { | ||
|
|
||
| if (!(point1 instanceof Point)) { | ||
| throw new Error('point1 is not a Point'); | ||
| } | ||
| if (!(point2 instanceof Point)) { | ||
| throw new Error('point2 is not a Point'); | ||
| } | ||
|
|
||
| let distanceX = (point2.x - point1.x) ** 2; | ||
| let distanceY = (point2.y - point1.y) ** 2; | ||
|
|
||
| const distance = Math.sqrt(distanceX + distanceY); | ||
| return distance; | ||
| } | ||
|
|
||
| const calculateJunctionPoint = (line1, line2) => { | ||
|
|
||
| if (!(line1 instanceof Line)) { | ||
| throw new Error('line1 is not a Line') | ||
| } | ||
|
|
||
| if (!(line2 instanceof Line)) { | ||
| throw new Error('line2 is not a Line') | ||
| } | ||
|
|
||
| if(!line1.slope){ | ||
| line1.calculateSlope() | ||
| } | ||
|
|
||
| if(!line2.slope){ | ||
| line2.calculateSlope() | ||
| } | ||
|
|
||
| if(!line1.n){ | ||
| line1.calculateNOfLineFunction() | ||
| } | ||
|
|
||
| if(!line2.n){ | ||
| line2.calculateNOfLineFunction() | ||
| } | ||
|
|
||
| if (line1.slope === line2.slope) { | ||
| if (line1.n === line2.n) { | ||
gemtechd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return true | ||
| } | ||
| else { | ||
| return false | ||
| } | ||
| } | ||
| else { | ||
| const x = (line1.n - line2.n) / (line2.slope - line1.slope) | ||
| const junctionPoint = line1.getPointByX(x); | ||
| return junctionPoint | ||
| } | ||
| } | ||
|
|
||
| const isPointOnLine = (line, point) => { | ||
|
|
||
| if (!(line instanceof Line)) { | ||
| throw new Error('line is not a Line') | ||
| } | ||
| if (!(point instanceof Point)) { | ||
| throw new Error('point is not a Point') | ||
| } | ||
| if(!line.slope){ | ||
| line.calculateSlope() | ||
| } | ||
|
|
||
| if(!line.n){ | ||
| line.calculateNOfLineFunction() | ||
| } | ||
|
|
||
|
|
||
| const proxyLine = new Line({ point1: line.point1, point2: point }) | ||
| proxyLine.calculateSlope() | ||
| if (line.slope === proxyLine.slope) { | ||
| proxyLine.calculateNOfLineFunction() | ||
| if (line.n === proxyLine.n) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens when the slope or n or both of them were not calculate yet? |
||
| return true | ||
| } | ||
| else { | ||
| return false; | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| module.exports = { | ||
| calculateDistance, | ||
| calculateJunctionPoint, | ||
| isPointOnLine | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,10 @@ | |
| "description": "practice unit tests in javascript", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "jest" | ||
| }, | ||
| "dependencies": { | ||
| "jest": "^29.7.0" | ||
| "test": "jest", | ||
| "test:coverage": "npm run test -- --coverage" | ||
| }, | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is jest now? |
||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/gemtechd/build-tests.git" | ||
|
|
@@ -19,5 +18,4 @@ | |
| "homepage": "https://github.com/gemtechd/build-tests#readme", | ||
| "author": "gemtechd", | ||
| "license": "ISC" | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens if slope is undefined?