題目
Day 4: Create a Rectangle Object
舉例輸入
4
5
舉例輸出
4
5
18
20
解析
系統給長寬,然後印出他的長寬、面積、邊長
function Rectangle(a, b) {
this.length = a;
this.width = b;
this.area = (this.width * this.length)
this.perimeter = (this.width + this.length) * 2;
}
function Rectangle(a, b) {
Rectangle.prototype.length=a;
Rectangle.prototype.width=b;
Rectangle.prototype.perimeter=2*(a+b);
Rectangle.prototype.area=a*b;
}
或著ES6的語法糖
class Rectangle{
constructor(l,w){
this.width = w;
this.length = l;
this.area= (this.width * this.length)
this.perimeter = (this.width + this.length) * 2;
}
}
當我吃飽撐著??
沒有,寫個筆記我們未來會用到
題目
Day 4: Count Objects
舉例輸入
5
1 1
2 3
3 3
3 4
4 5
舉例輸出
2
解析
系統丟入一個陣列,該陣列每個元素有x、y
如果XY相同則token+1
function getCount(objects) {
let i = 0;
objects.forEach(obj => {
if (obj.x == obj.y) {
i += 1;
}
});
return i;
}
有沒有更風騷的寫法??
有的
反正我們就是要x==y的資料嘛
function getCount(objects) {
return objects.filter(object => object.x == object.y).length;
}