جواب «Nobody» چالش 2 از جلسهی 18
سؤال
تابع firstLong اولین اسمی را برمیگرداند که بیشتر از ۴ حرف دارد. ولی find شاید چیزی پیدا نکند و undefined بدهد، برای همین return خط قرمز دارد.
با ?? کاری کن که اگر اسمی پیدا نشد، تابع "nobody" برگرداند.
برنامه باید nobody و Samira را نشان بدهد.
راهنما
خط return اینطور میشود: return found ?? "nobody";
یک جواب درست
<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">
function firstLong(names: string[]): string {
const found = names.find((n) => n.length > 4);
return found ?? "nobody";
}
show(firstLong(["Sara", "Ali"]));
show(firstLong(["Ali", "Samira"]));
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن