Skip to content
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

[Ch09] before vs. after #9

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions ch9/01-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ const scenario = {
delay: 40,
}

const distanceTravelled = (scenario, time) => {
let result
let acc = scenario.primaryForce / scenario.mass // (a = F / m)
let primaryTime = Math.min(time, scenario.delay)
result = 0.5 * acc * primaryTime ** 2
let secondaryTime = time - scenario.delay
if (secondaryTime > 0) {
let primaryVelocity = acc * scenario.delay
acc = (scenario.primaryForce + scenario.secondaryForce) / scenario.mass
result += primaryVelocity * secondaryTime + 0.5 * acc * secondaryTime ** 2
}
return result
// 가속도 = 힘 / 무게
const acceleration = (force, mass) => force / mass

// 가속운동시 거리 = 1/2 * a * t^2
const acceleratedDistance = (acc, time) => (acc * time ** 2) / 2

// 등속운동시 거리 = 속도 * 시간
const sameVelocityDistance = (velocity, time) => velocity * time

const distanceTravelled = ({ primaryForce, secondaryForce, mass, delay }, time) => {
const primaryAcceleration = acceleration(primaryForce, mass)
const secondaryAcceleration = acceleration(primaryForce + secondaryForce, mass)
const secondaryTime = Math.max(time - delay, 0)
return (
acceleratedDistance(primaryAcceleration, Math.min(time, delay)) +
acceleratedDistance(secondaryAcceleration, secondaryTime) +
sameVelocityDistance(primaryAcceleration * delay, secondaryTime)
)
}

console.log(distanceTravelled(scenario, 100))
7 changes: 4 additions & 3 deletions ch9/01-2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const discount = (inputValue, quantity) => {
if (inputValue > 50) inputValue = inputValue - 2
if (quantity > 100) inputValue = inputValue - 1
return inputValue
let result = inputValue
if (inputValue > 50) result = result - 2
if (quantity > 100) result = result - 1
return result
}
console.log(discount(40, 90))
console.log(discount(70, 90))
Expand Down
15 changes: 8 additions & 7 deletions ch9/02.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class Organization {
#name
#title
#country
constructor(data) {
this.#name = data.name
this.#title = data.title
this.#country = data.country
}
get name() {
return this.#name
get title() {
return this.#title
}
set name(aString) {
this.#name = aString
set title(aString) {
this.#title = aString
}
get country() {
return this.#country
Expand All @@ -18,4 +18,5 @@ class Organization {
this.#country = aCountry
}
}
const organization = new Organization({ name: '애크미 구스베리', country: 'GB' })
const organization = new Organization({ title: '애크미 구스베리', country: 'GB' })
console.log(organization.title)
8 changes: 5 additions & 3 deletions ch9/03-1.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
class ProductionPlan {
#production = 0
#adjustments = []
get production() {
return this.#production
return this.calculatedProduction
}
get calculatedProduction() {
return this.#adjustments.reduce((sum, a) => sum + a.amount, 0)
}

applyAdjustment(anAdjustment) {
this.#adjustments.push(anAdjustment)
this.#production += anAdjustment.amount
}
}

Expand Down
12 changes: 8 additions & 4 deletions ch9/03-2.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
class ProductionPlan {
#production = 0
#initialProduction
#adjustments = []
constructor(production) {
this.#production = production
this.#initialProduction = production
this.#adjustments = []
}
get production() {
return this.#production
return this.#initialProduction + this.calculatedProductionAccumulator
}
get calculatedProductionAccumulator() {
return this.#adjustments.reduce((sum, a) => sum + a.amount, 0)
}

applyAdjustment(anAdjustment) {
this.#adjustments.push(anAdjustment)
this.#production += anAdjustment.amount
}
}

Expand Down
42 changes: 35 additions & 7 deletions ch9/04.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/* import { assert } from 'chai'
import { it } from 'mocha' */
class TelephoneNumber {
#areaCode
#number
constructor(areaCode, number) {
this.#areaCode = areaCode
this.#number = number
}
get areaCode() {
return this.#areaCode
}
Expand All @@ -13,28 +19,50 @@ class TelephoneNumber {
set number(arg) {
this.#number = arg
}
equals(other) {
if (!(other instanceof TelephoneNumber)) return false
return this.#areaCode === other.#areaCode && this.#number === other.#number
}
}

class Person {
#telephoneNumber
constructor() {
this.#telephoneNumber = new TelephoneNumber()
this.#telephoneNumber = new TelephoneNumber('', '')
}
get telephone() {
return this.#telephoneNumber
}
get officeAreaCode() {
return this.#telephoneNumber.areaCode
}
set officeAreaCode(arg) {
this.#telephoneNumber.areaCode = arg
this.#telephoneNumber = new TelephoneNumber(arg, this.officeNumber)
}
get officeNumber() {
return this.#telephoneNumber.number
}
set officeNumber(arg) {
this.#telephoneNumber.number = arg
this.#telephoneNumber = new TelephoneNumber(this.officeAreaCode, arg)
}
}

const p = new Person()
p.officeAreaCode = '312'
p.officeNumber = '555-0142'
console.log(p.officeAreaCode, p.officeNumber)
const p1 = new Person()
p1.officeAreaCode = '312'
console.log(p1.officeAreaCode, p1.officeNumber)

p1.officeNumber = '555-0142'
console.log(p1.officeAreaCode, p1.officeNumber)

/*
const p2 = new Person()
p2.officeAreaCode = '456'
p2.officeNumber = '777-0342'

console.log(p2.officeAreaCode, p2.officeNumber)
console.log(p1.officeAreaCode, p1.officeNumber)
console.log(p1.telephone.areaCode, p2.telephone.areaCode)
*/
/* it('telephone equals', () => {
assert(new TelephoneNumber('312', '555-0142').equals(new TelephoneNumber('312', '555-0142')))
}) */
13 changes: 11 additions & 2 deletions ch9/05.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ class Customer {
}
}

const findCustomer = id => _repositoryData.customers.get(id)
class Order {
#number
#customer
constructor(data) {
this.#number = data.number
this.#customer = new Customer(data.customer)
this.#customer = registerCustomer(data.customer)
}
get customer() {
return this.#customer
return findCustomer(this.#customer)
}
}

const _repositoryData = { customers: new Map() }
const registerCustomer = id => {
if (!_repositoryData.customers.has(id)) {
_repositoryData.customers.set(id, new Customer(id))
}
return id
}

const o = new Order({ number: 1, customer: 'a' })
console.log(o.customer.id)