2021.10.25
before
function trackSummary(points) {
const totalTime = calculateTime()
const totalDistance = calculateDistance()
const pace = totalTime / 60 / totalDistance
return {
time: totalTime,
distance: totalDistance,
pace: pace,
}
function calculateDistance() { // 총 거리 계산
let result = 0
for (let i = 0; i < points.length; i++) {
result += distance(points[i-1], points[i])
}
return result
}
function distance(p1, p2) {
const EARTH_RADIUS = 3959
const dLat = radians(p2.lat) - radians(p1.lat)
const dLon = radians(p2.lon) - radians(p1.lon)
const a = Math.pow(Math.sin(dLat/ 2), 2)
+ Math.cos(radians(p2.lat))
+ Math.cos(radians(p1.lat))
+ Math.pow(Math.sin(dLon / 2), 2)
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
return EARTH_RADIUS * c
} // 두 지점의 거리 계산
function radians(degrees) {
return degrees * Math.PI / 180
} // 라디안 값으로 변환
function calculateTime() { ... } // 총 시간 계산
}
after
function trackSummary(points) {
const totalTime = calculateTime()
const pace = totalTime / 60 / totalDistance(points)
return {
time: totalTime,
distance: totalDistance(points),
pace: pace,
}
}
function totalDistance(points) {
let result = 0
for (let i = 0; i < points.length; i++) {
result += distance(points[i-1], points[i])
}
return result
}
function distance(p1, p2) { ... } // 두 지점의 거리 계산
function radians(degrees) { ... } // 라디안 값으로 변환
function calculateTime() { ... } // 총 시간 계산
before
class Customer {
get plan() {
return this._plan;
}
get discountRate() {
return this._discountRate;
}
}
after
class Customer {
get plan() {
return this._plan;
}
get discountRate() {
return this.plan.discountRate;
}
}
before
result.push(`<p>제목: ${person.photo.title}</p>`);
result.concat(photoData(person.photo));
function photoData(aPhoto) {
return [`<p>위치: ${aPhoto.location}</p>`, `<p>위치: ${aPhoto.data}</p>`];
}
after
result.concat(photoData(person.photo));
function photoData(aPhoto) {
return [`<p>제목: ${aPhoto.title}</p>`, `<p>위치: ${aPhoto.location}</p>`, `<p>위치: ${aPhoto.data}</p>`];
}
before
emitPhotoData(outStream, person.photo);
function emitPhotoData(outStream, photo) {
outStream.write(`<p>제목: ${photo.title}</p>`);
outStream.write(`<p>위치: ${photo.location}</p>`);
}
after
emitPhotoData(outStream, person.photo);
outStream.write(`<p>위치: ${person.photo.location}</p>`);
function emitPhotoData(outStream, photo) {
outStream.write(`<p>제목: ${photo.title}</p>`);
}
before
let appliesToMass = false;
for (const s of states) {
if (s === 'MA') appliesToMass = true;
}
after
appliesToMass = states.includes('MA');
before
const pricingPlan = retrievePricingPlan();
const order = retrieveOrder();
let charge;
const chargePerUnit = pricingPlan.unit;
after
const pricingPlan = retrievePricingPlan();
const chargePerUnit = pricingPlan.unit;
const order = retrieveOrder();
let charge;
before
let averageAge = 0;
let totalSalary = 0;
for (const p of people) {
averageAge += p.age;
totalSalary += p.salary;
}
averageAge = averageAge / people.length;
after
let totalSalary = 0;
for (const p of people) {
totalSalary += p.salary;
}
let averageAge = 0;
for (const p of people) {
averageAge += p.age;
}
averageAge = averageAge / people.length;
before
const name = [];
for (const i of input) {
if (i.job === 'programmer') name.push(i.name);
}
after
const names = input.filter((i) => i.job === 'programmer').map((i) => i.map);