Give a greeting function a default name of friend and call it with no argument.
Acceptance criteria
- 1.#out should show Hello friend.
JavaScript Tutorial · JavaScript Functions
These 10 tasks go with the JavaScript Default Parameters chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “JavaScript default parameters give a parameter a value to use when no argument is passed.” .
1.Add a defaultexercise · easy · 10 min
Give a greeting function a default name of friend and call it with no argument.
Passes when: #out should show Hello friend.
2.Override the defaultexercise · easy · 10 min · practice plan
Call the same greeting function with the name Ada.
Passes when: #out should show Hello Ada.
3.A default quantityexercise · easy · 10 min · practice plan
Give quantity a default of 1 and show the total for a price of 50.
Passes when: #out should show 50.
4.Build a default from another parameterexercise · easy · 10 min · practice plan
Make height default to width so one argument gives a square. Show it for 6.
Passes when: #out should show 36.
5.A default empty arrayexercise · medium · 10 min · practice plan
Default the list to an empty array so counting never fails. Show the count with no argument.
Passes when: #out should show 0.
6.Fix the missing defaultbug fix · easy · 8 min
With no default the name becomes undefined.
Passes when: #out should show Hello friend.
7.Fix the colon used instead of equalsbug fix · easy · 8 min · practice plan
A default is written with an equals sign.
Passes when: #out should show 50.
8.Fix the default that is never usedbug fix · easy · 8 min · practice plan
Passing null does not trigger the default.
Passes when: #out should show Hello friend.
9.Fix the default in the wrong placebug fix · easy · 8 min · practice plan
The default is on the first parameter, so the value lands wrongly.
Passes when: #out should show 36.
10.Fix the default that crashesbug fix · easy · 8 min · practice plan
Without a default the length of undefined cannot be read.
Passes when: #out should show 0.
Between them these tasks cover Default parameters, Optional arguments and undefined handling.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.