Use an IIFE to work out 6 times 7 and store the answer.
Acceptance criteria
- 1.#out should show 42.
JavaScript Tutorial · JavaScript Functions
These 10 tasks go with the JavaScript IIFE 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: “A JavaScript IIFE is a function that runs immediately after it is defined.” Read the chapter.
1.Write an IIFEexercise · easy · 10 min
Use an IIFE to work out 6 times 7 and store the answer.
Passes when: #out should show 42.
2.Pass an argumentexercise · easy · 10 min · practice plan
Pass the name Ada into an IIFE and return a greeting.
Passes when: #out should show Hello Ada.
3.Keep a variable privateexercise · easy · 10 min · practice plan
Show that a variable inside an IIFE cannot be seen outside.
Passes when: #out should show undefined.
4.Return an objectexercise · easy · 10 min · practice plan
Return a config object from an IIFE and show its version.
Passes when: #out should show 1.0.
5.An arrow IIFEexercise · medium · 10 min · practice plan
Write the same idea as an arrow IIFE returning the word ready.
Passes when: #out should show ready.
6.Fix the IIFE that never runsbug fix · easy · 8 min
Without the final brackets the function is stored, not called.
Passes when: #out should show 42.
7.Fix the argument in the wrong bracketsbug fix · easy · 8 min · practice plan
The argument belongs in the calling brackets.
Passes when: #out should show Hello Ada.
8.Fix the leaked variablebug fix · easy · 8 min · practice plan
The variable is declared outside, so it is not private.
Passes when: #out should show undefined.
9.Fix the missing returnbug fix · easy · 8 min · practice plan
The IIFE builds the object but never gives it back.
Passes when: #out should show 1.0.
10.Fix the arrow IIFE syntaxbug fix · easy · 8 min · practice plan
The arrow function is never invoked.
Passes when: #out should show ready.
Between them these tasks cover Immediately invoked functions, Private scope and Module pattern.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.