Many languages have a switch statement in addition to the conditional if statement. This is a specialized version of if, designed for certain cases. For example, you can use it where there is an if else chain with equality checks:
if (status === 'processing') {
// First
} else if (status === 'paid') {
// Second
} else if (status === 'new') {
// Third
} else {
// Fourth
}
This compound check has one distinctive feature: each branch here is a check of the value of the status
variable. 'Switch' allows you to write this code in a shorter and more expressive way:
switch (status) {
case 'processing': // status === processing
// First
break;
case 'paid': // status === paid
// Second
break;
case 'new': // status === new
// Third
break;
default: // else
// Fourth
}
Switch is a rather advanced construct considering the number of elements it consists of:
switch
. A variable whose values switch
will use to select a behavior. And curly brackets for behavior branchescase
and default
constructs, where the behavior for different values of the variable is described. Each case
corresponds to if
in the example above. default
is a special condition corresponding to the else
branch in conditionals. Neither else
nor default
is necessary (but the linter will always ask for it)break
is needed to prevent it from "falling through". If break
is omitted, then the program will continue to the next case
statement after the required case
, and carry on until the next break
or the end of the switchThe curly brackets in switch
don't specify a code block unlike elsewhere. Only the syntax shown above is acceptable. In other words, you can use case
or default
there. But inside each case
(and default
), the situations are different. You can execute any arbitrary code here:
switch (count) {
case 1:
// Do something useful
break;
case 2:
// Do something useful
break;
default:
// Do something
}
Sometimes the output of the case
is the end of a function containing switch. If this happens, you need to return it outside somehow. There are two ways to solve this problem.
The first way. Create a variable before switch, fill it in case, and then return the value of that variable outside at the end.
(count) => {
// Declare a variable
let result;
// Fill it
switch (count) {
case 1:
result = 'one';
break;
case 2:
result = 'two';
break;
default:
result = null;
}
// Return
return result;
};
The second way is easier and shorter. Instead of creating a variable, case allows us to make a regular function return inside it. And since no code is executed after return
, we can get rid of break
:
(count) => {
switch (count) {
case 1:
return 'one';
case 2:
return 'two';
default:
return null;
}
};
Though switch is present in the code, you always can find a way to avoid it. The key benefit of using it is that it better expresses the programmer's intention when checking the values of a particular variable. Although the code becomes a bit longer, it's easier to read compared to else if blocks.
Write the getNumberExplanation()
function that describes an input number passed to it. If there is no explanation available, it returns just a number
:
getNumberExplanation(8); // just a number
// Only the following numbers have descriptions
getNumberExplanation(666); // devil number
getNumberExplanation(42); // answer for everything
getNumberExplanation(7); // prime number
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.
Your exercise will be checked with these tests:
1import { test, expect } from '@jest/globals';
2import f from './index.js';
3
4test('test', () => {
5 expect(f(0)).toBe('just a number');
6 expect(f(666)).toBe('devil number');
7 expect(f(42)).toBe('answer for everything');
8 expect(f(7)).toBe('prime number');
9});
10
Teacher's solution will be available in: