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

Make It Optional چالش 1 از جلسه‌ی 7، آسان

تابع greet لقب را فقط وقتی می‌نویسد که داده شده باشد. ولی خط آخر قرمز است، چون title هنوز اجباری است.
با یک ? ورودی title را اختیاری کن. برنامه باید دو خط نشان بدهد: Dr Sara و Ali.

<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 greet(name: string, title: string): string {
  if (title) {
    return title + " " + name;
  }
  return name;
}
show(greet("Sara", "Dr"));
show(greet("Ali"));
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 7: ورودیِ شاید · چالش بعدی: The Usual Price

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