Every Pet Is Welcome چالش 4 از جلسهی 23، متوسط
تابع loud و آرایهی pets فقط Dog قبول میکنند. برای همین گربه خط قرمز میشود.
کاری کن هر حیوانی قبول شود:
- نوع ورودی loud را از Dog به والدش عوض کن.
- نوع آرایهی pets را هم همینطور.
کلاسها را دست نزن.
برنامه باید بنویسد: Woof! و در خط بعد Meow!
<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 Animal {
sound(): string {
return "...";
}
}
class Dog extends Animal {
sound(): string {
return "Woof";
}
fetch(): string {
return "Got the ball!";
}
}
class Cat extends Animal {
sound(): string {
return "Meow";
}
}
function loud(a: Dog): string {
return a.sound() + "!";
}
const pets: Dog[] = [new Dog(), new Cat()];
for (const pet of pets) {
show(loud(pet));
}
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 23: خانوادهها · چالش بعدی: Find the Red Lines
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js