بتل کد Battle Codeدارد باز می‌شود…

The Square Knows Its Area چالش 3 از جلسه‌ی 23، متوسط

هر Shape متد area دارد، ولی شکلِ بی‌نام‌ونشان مساحتی ندارد و 0 برمی‌گرداند.
Square فرزند Shape است، اما هنوز همان 0 را می‌گوید.
در Square متد area(): number را دوباره بنویس تا side * side را برگرداند. describe را دست نزن.
برنامه باید بنویسد: Square area 16، بعد Square area 25 و بعد Dot area 0

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
class Shape {
  constructor(public name: string) {}

  area(): number {
    return 0;
  }

  describe(): string {
    return this.name + " area " + this.area();
  }
}

class Square extends Shape {
  constructor(public side: number) {
    super("Square");
  }

  // rewrite area here
}

show(new Square(4).describe());
show(new Square(5).describe());
show(new Shape("Dot").describe());
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 23: خانواده‌ها · چالش بعدی: Every Pet Is Welcome

بیشتر بخوان: جاوااسکریپت چیست؟ از دکمه‌ی وب تا Node.js