- What does a while loop do
- What is the difference to do-while
- Syntax
while(){}
anddo{} while()
- Break and continue
- What is the switch statement
- Syntax
switch(){}
- What is the
default
case for - What does
break
do - Why is break so important when using switch
The following topics are out of scope because they are covered by another concept exercise.
- For loops
- Array loops (for...in, forEach, map)
The Concepts this exercise unlocks are:
while-loops
conditionals-switch
comparison
for writing the condition in the loop headerconditionals
because they introduced the student to the concept of conditional executionarrays
because they are used to loop over them in the exercise
This exercise could benefit from the following rules in the analyzer. The comment types mentioned below only serve as a proposal.
-
timeToMixJuice
essential
: Verify the student used a switch statement. Would be nice if we could give different feedback depending on what the student used instead. If it was if-else, comment that switch is better suited for so many different variants. If an object was used, comment that this is nice but the goal is to practice switch.essential
: Verify that there are 5 cases and a default case in the switch statement to make sure the student did not tailor their code for the test cases (e.g., used more cases instead of default).actionable
: If the student usedbreak
, comment to use earlyreturn
s instead to avoid assigning to a helper variable first and then returning that variable.celebratory
: Comment something nice when a student used grouped case statements.case 'Energizer': case 'Green Garden': return 1.5;
-
limesToCut
- A solution that uses
if (limes.length < 0) break;
instead of combining the conditions should be considered equally correct to the exemplar solution. The version in the exemplar file is shorter but the break version emphasizes that there is a special edge case. essential
: Verify thatwhile
was used.essential
: If a helper function was used for the switch statement, check that is was not exported.actionable
: If the student wroteif (limes.length < 0) return limesCut
, comment that the duplication ofreturn limesCut
can be avoided by using break there instead of return.actionable
: Tell the student to use a helper function to wrap the switch statement for readability if he/she did not do that.informative
: If the student used a counter to iterate over the array, show a comment about aboutshift
.informative
: Remind the student about++
if it was not used to increment the lime counter.informative
: Check whether a shorthand assignment+=
was used to increase the loop counter.informative
: Ifdefault
was included in the switch statement, remind the student that it is optional and not needed in the scope of the task.celebratory
: Make a positive remark if the student used a helper function to wrap the switch statement.celebratory
: Celebrate if the student used++
and+=
.
- A solution that uses
-
remainingOrders
essential
: Verify that do-while was used. If while was used instead, say that do-while is a better fit because there is always at least one iteration (becausetimeLeft
is always > 0) and the condition can best be checked after running the code.essential
: VerifytimeToMixJuice
was reused instead of duplicating the code.- Most of the points from task 2 also apply here. Check what can be reused.
The exercise is inspired by the Master Mixologist Exercise in the Swift track. The original exercise also included for loops which is covered by a different exercise in the JavaScript track. The tasks were adapted accordingly which also simplified them. The alcohol was replaced and the name was changed to match the new story.
The exercise would benefit from another task to practice continue
.