جواب «Find the Red Lines» چالش 5 از جلسهی 16
سؤال
این برنامه شناسهها را نشان میدهد: متن را با حروف بزرگ و عدد را بعد از No. .
دو اشتباه دارد که چند خط قرمز ساختهاند. پیدایشان کن:
- یکی در سؤالی است که با typeof پرسیده شده.
- یکی در مقداری است که در یک Id گذاشته شده. آن خط را پاک کن.
برنامه باید No. 7 و Code AB12 را نشان بدهد.
راهنما
جواب typeof برای متن "string" است، نه "text". و true نه عدد است نه متن: خط third را پاک کن.
یک جواب درست
<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">
type Id = number | string;
function showId(id: Id): string {
if (typeof id === "string") {
return "Code " + id.toUpperCase();
}
return "No. " + id;
}
const first: Id = 7;
const second: Id = "ab12";
show(showId(first));
show(showId(second));
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن