как найти сумму элементов объектов,но только через петлю (цикл) for..../тоже самое только через петлю (цикл) for.?
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.hypot(dx, dy);
}}
const p1 = new Point(5, 5);
const p2 = new Point(10, 10);
p1.distance;
p2.distance;
console.log(Point.distance(p1, p2));
Ответы (1 шт):
Автор решения: Igor
→ Ссылка
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.hypot(dx, dy);
}
static add(a, b) {
return Point.addAll(a, b);
}
static addAll() {
let x = 0;
let y = 0;
for (let i = 0; i < arguments.length; i++) {
x += arguments[i].x;
y += arguments[i].y;
}
return new Point(x, y);
}
}
const p1 = new Point(5, 5);
const p2 = new Point(10, 10);
console.log(Point.distance(p1, p2));
console.log(Point.add(p1, p2));
console.log(Point.addAll(new Point(1, -1), new Point(2, -2), new Point(3, -3)));