JavaScript: The else if construct
The getTypeOfSentence() function from the previous lesson distinguishes only between questions and regular sentences. Let's try to add support for exclamatory sentences:
const getTypeOfSentence = (sentence) => {
const lastChar = sentence[sentence.length - 1];
let sentenceType;
if (lastChar === '!') {
sentenceType = 'exclamation';
} else {
sentenceType = 'normal';
}
if (lastChar === '?') {
sentenceType = 'question';
}
return `Sentence is ${sentenceType}`;
};
getTypeOfSentence('Who?'); // Sentence is question
getTypeOfSentence('No'); // Sentence is normal
getTypeOfSentence('No!'); // Sentence is exclamationWe added one more check. Technically the function works, but from a semantics standpoint there are problems.
- The check for a question mark runs in any case, even if an exclamation mark has already been detected.
- The
elsebranch is described for the first condition only, but not for the second.
It would be more correct to use one more feature of the conditional construct:
const getTypeOfSentence = (sentence) => {
const lastChar = sentence[sentence.length - 1];
let sentenceType;
if (lastChar === '?') {
sentenceType = 'question';
} else if (lastChar === '!') {
sentenceType = 'exclamation';
} else {
sentenceType = 'normal';
}
return `Sentence is ${sentenceType}`;
};
getTypeOfSentence('Who?'); // Sentence is question
getTypeOfSentence('No'); // Sentence is normal
getTypeOfSentence('No!'); // Sentence is exclamationNow all conditions are arranged into a single construct. else if means "if the previous condition was not met, but the current one is".
┌─────────────────┐
│ condition 1? │
└────┬────────┬───┘
true │ │ false
↓ ↓
┌──────────┐ ┌─────────────────┐
│ if body │ │ condition 2? │
└──────────┘ └────┬────────┬───┘
true │ │ false
↓ ↓
┌──────────────┐ ┌────────────┐
│ else if body │ │ else body │
└──────────────┘ └────────────┘The function logic results in the following scheme:
- if the last character is
?, then'question' - otherwise, if the last character is
!, then'exclamation' - otherwise
'normal'
Only one of the code blocks belonging to the whole if construct will be executed.
Instructions
Implement the getTrafficLightAction(color) function, which takes a traffic light color and returns what the driver should do:
'green'→'go''yellow'→'slow down''red'→'stop'- any other color →
'unknown'
getTrafficLightAction('green'); // => 'go'
getTrafficLightAction('red'); // => 'stop'
getTrafficLightAction('purple'); // => 'unknown'Tips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
JavaScript: The else if construct
The getTypeOfSentence() function from the previous lesson distinguishes only between questions and regular sentences. Let's try to add support for exclamatory sentences:
const getTypeOfSentence = (sentence) => {
const lastChar = sentence[sentence.length - 1];
let sentenceType;
if (lastChar === '!') {
sentenceType = 'exclamation';
} else {
sentenceType = 'normal';
}
if (lastChar === '?') {
sentenceType = 'question';
}
return `Sentence is ${sentenceType}`;
};
getTypeOfSentence('Who?'); // Sentence is question
getTypeOfSentence('No'); // Sentence is normal
getTypeOfSentence('No!'); // Sentence is exclamationWe added one more check. Technically the function works, but from a semantics standpoint there are problems.
- The check for a question mark runs in any case, even if an exclamation mark has already been detected.
- The
elsebranch is described for the first condition only, but not for the second.
It would be more correct to use one more feature of the conditional construct:
const getTypeOfSentence = (sentence) => {
const lastChar = sentence[sentence.length - 1];
let sentenceType;
if (lastChar === '?') {
sentenceType = 'question';
} else if (lastChar === '!') {
sentenceType = 'exclamation';
} else {
sentenceType = 'normal';
}
return `Sentence is ${sentenceType}`;
};
getTypeOfSentence('Who?'); // Sentence is question
getTypeOfSentence('No'); // Sentence is normal
getTypeOfSentence('No!'); // Sentence is exclamationNow all conditions are arranged into a single construct. else if means "if the previous condition was not met, but the current one is".
┌─────────────────┐
│ condition 1? │
└────┬────────┬───┘
true │ │ false
↓ ↓
┌──────────┐ ┌─────────────────┐
│ if body │ │ condition 2? │
└──────────┘ └────┬────────┬───┘
true │ │ false
↓ ↓
┌──────────────┐ ┌────────────┐
│ else if body │ │ else body │
└──────────────┘ └────────────┘The function logic results in the following scheme:
- if the last character is
?, then'question' - otherwise, if the last character is
!, then'exclamation' - otherwise
'normal'
Only one of the code blocks belonging to the whole if construct will be executed.
Instructions
Implement the getTrafficLightAction(color) function, which takes a traffic light color and returns what the driver should do:
'green'→'go''yellow'→'slow down''red'→'stop'- any other color →
'unknown'
getTrafficLightAction('green'); // => 'go'
getTrafficLightAction('red'); // => 'stop'
getTrafficLightAction('purple'); // => 'unknown'Tips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
import { expect, test } from 'vitest';
import f from './index.js';
test('else if', () => {
expect(f('green')).toBe('go');
expect(f('yellow')).toBe('slow down');
expect(f('red')).toBe('stop');
expect(f('blue')).toBe('unknown');
expect(f('purple')).toBe('unknown');
});Teacher's solution will be available in:
20:00
