Introduction
Bob is a lackadaisical teenager.
He likes to think that he’s very cool.
And he definitely doesn’t get excited about things.
That wouldn’t be cool.
When people talk to him, his responses are pretty limited.
Instructions
Instructions
Your task is to determine what Bob will reply to someone when they say something to him or ask him a question.
Bob only ever answers one of five things:
- “Sure.”
This is his response if you ask him a question, such as “How are you?”
The convention used for questions is that it ends with a question mark.
- “Whoa, chill out!”
This is his answer if you YELL AT HIM.
The convention used for yelling is ALL CAPITAL LETTERS.
- “Calm down, I know what I’m doing!”
This is what he says if you yell a question at him.
- “Fine. Be that way!”
This is how he responds to silence.
The convention used for silence is nothing, or various combinations of whitespace characters.
- “Whatever.”
This is what he answers to anything else.
Dig Deeper
If statements
if statements
export function hey(message) {
const speech = message.trimEnd();
if (speech == "") {
return "Fine. Be that way!";
}
const isQuestion = speech.endsWith("?");
const isShout = /[A-Z]{1}/.test(speech) && speech == speech.toUpperCase();
if (isShout) {
return isQuestion
? "Calm down, I know what I'm doing!"
: "Whoa, chill out!";
}
if (isQuestion) {
return "Sure.";
}
return "Whatever.";
In this approach you have a series of if statements using the private methods to evaluate the conditions.
As soon as the right condition is found, the correct response is returned.
Note that there are no `else if` or `else` statements.
If an `if` statement can return, then an `else if` or `else` is not needed.
Execution will either return or will continue to the next statement anyway.
The String trimEnd method is applied to the input to eliminate any whitespace at the end of the input.
If the string has no characters left, it returns the response for saying nothing.
The String method endsWith is used to determine if the input ends with a question mark.
Note that a `null` or `undefined` `String` would be different from a `String` of all whitespace.
A `null` or `undefined` `String` would raise a `TypeError` if `trimEnd` were applied to it.
The first half of the shout condition
/[A-Z]{1}/.test(speech);
is constructed from a regular expression pattern to ensure there is at least one uppercase letter character in the String.
This is because the second half of the condition tests that the uppercased input is the same as the input.
If the input were only "123" it would equal itself uppercased, but without letters it would not be a shout.
The uppercasing is done by using the String method toUpperCase.
If isShout is true, a ternary operator is used to return the response for whether isQuestion is also true,
or only isShout is true.
Shortening
Note that when the body of an if statement is a short single line, both the test expression and the body could be put on the same line, like so
if (speech == '') return 'Fine. Be that way!';
It may not comply with some coding styles, but your team preferences may allow it.
Switch statement
switch statement
export function hey(message) {
const speech = message.trim();
if (speech == '') {
return 'Fine. Be that way!';
}
const isQuestion = speech.endsWith('?');
const isShout = /[A-Z]{1}/.test(speech) && speech == speech.toUpperCase();
switch (true) {
case isQuestion && isShout:
return "Calm down, I know what I'm doing!";
case isShout:
return 'Whoa, chill out!';
case isQuestion:
return 'Sure.';
default:
return 'Whatever.';
}
}
In this approach you use a switch statement to test if there is a question or a shout.
The switch returns the right response for a question, shout, shouted question, or for not being a shout or question.
The String trimEnd method is applied to the input to eliminate any whitespace at the end of the input.
If the string has no characters left, it returns the response for saying nothing.
Note that a `null` or `undefined` `String` would be different from a `String` of all whitespace.
A `null` or `undefined` `String` would raise a `TypeError` if `trimEnd` were applied to it.
The first half of the shout condition
/[A-Z]{1}/.test(speech);
is constructed from a regular expression pattern to ensure there is at least one uppercase letter character in the String.
This is because the second half of the condition tests that the uppercased input is the same as the input.
If the input were only "123" it would equal itself uppercased, but without letters it would not be a shout.
The uppercasing is done by using the String method toUpperCase.
The isQuestion and isShout values are tested in a switch.
If neither isQuestion and isShout are true, the default arm of the switch returns the response when the input is neither a question nor a shout.
Shortening
Note that when the body of an if statement is a short single line, both the test expression and the body could be put on the same line, like so
if (input == '') return 'Fine. Be that way!';
It may not comply with some coding styles, but your team preferences may allow it.
Answer array
Answer array
const answers = [
'Whatever.',
'Sure.',
'Whoa, chill out!',
"Calm down, I know what I'm doing!",
];
export function hey(message) {
const speech = message.trimEnd();
if (speech == '') {
return 'Fine. Be that way!';
}
const isQuestion = speech.endsWith('?') ? 1 : 0;
const isShout =
/[A-Z]{1}/.test(speech) && speech == speech.toUpperCase() ? 2 : 0;
return answers[isQuestion + isShout];
}
In this approach you define an array that contains Bob’s answers, and each condition is given a score.
The correct answer is selected from the array by using the score as the array index.
The String trimEnd method is applied to the input to eliminate any whitespace at the end of the input.
If the string has no characters left, it returns the response for saying nothing.
Note that a `null` or `undefined` `String` would be different from a `String` of all whitespace.
A `null` or `undefined` `String` would raise a `TypeError` if `trimEnd` were applied to it.
The first half of the shout condition
/[A-Z]{1}/.test(speech)
is constructed from a regular expression pattern to ensure there is at least one uppercase letter character in the String.
This is because the second half of the condition tests that the uppercased input is the same as the input.
If the input were only "123" it would equal itself uppercased, but without letters it would not be a shout.
The uppercasing is done by using the String method toUpperCase.
The conditions of being a question and being a shout are assigned scores through the use of the ternary operator.
For example, giving a question a score of 1 would use an index of 1 to get the element from the answers array, which is "Sure.".
| isShout | isQuestion | Index | Answer |
|---|
false | false | 0 + 0 = 0 | "Whatever." |
false | true | 0 + 1 = 1 | "Sure." |
true | false | 2 + 0 = 2 | "Whoa, chill out!" |
true | true | 2 + 1 = 3 | "Calm down, I know what I'm doing!" |
Shortening
Note that when the body of an if statement is a short single line, both the test expression and the body could be put on the same line, like so
if (speech == '') return 'Fine. Be that way!';
It may not comply with some coding styles, but your team preferences may allow it.
Source: Exercism javascript/bob