Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 09fb1eb

Browse files
Kyu 8 | Be Concise I - The Ternary Operator
1 parent c6f5f47 commit 09fb1eb

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

‎src/main/java/kyu8/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Kata Found realized
3838

3939
- [Basic variable assignment](basicVariableAssignment)
4040

41+
- [Be Concise I - The Ternary Operator](beConciseITheTernaryOperator)
42+
4143
- [Find Nearest square number](findNearestSquareNumber)
4244

4345

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package kyu8.beConciseITheTernaryOperator;
2+
3+
public class C {
4+
static String describeAge(int a) {
5+
return "You're a(n) " + (a < 13 ? "kid" : a < 18 ? "teenager" : a < 65 ? "adult" : "elderly");
6+
}
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Be Concise I - The Ternary Operator
2+
3+
You are given a function ```describeAge``` / ```describe_age``` that takes a parameter ```age``` (which will always be a
4+
positive integer) and does the following:
5+
6+
1. If the age is ```12``` or lower, it ```return "You're a(n) kid"```
7+
2. If the age is anything between ```13``` and ```17``` (inclusive), it ```return "You're a(n) teenager"```
8+
3. If the age is anything between ```18``` and ```64``` (inclusive), it ```return "You're a(n) adult"```
9+
4. If the age is ```65``` or above, it ```return "You're a(n) elderly"```
10+
11+
Your task is to shorten the code as much as possible. Note that submitting the given code will not work because there is
12+
a character limit of 137.
13+
14+
I'll give you a few hints:
15+
16+
1. The title itself is a hint - if you're not sure what to do, always research any terminology in this description that
17+
you have not heard of!
18+
2. Don't you think the whole ```"You're a(n) <insert_something_here>"``` is very repetitive? ;) Perhaps we can shorten
19+
it?
20+
3. Write everything in one line, ```\n``` and other whitespaces counts.
21+
22+
**Whatever you do, do not change what the function does.** Good luck :)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package kyu8.beConciseITheTernaryOperator;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class testC {
8+
@Test
9+
public void basicTests() {
10+
assertEquals("You're a(n) kid", C.describeAge(9));
11+
assertEquals("You're a(n) kid", C.describeAge(10));
12+
assertEquals("You're a(n) kid", C.describeAge(11));
13+
assertEquals("You're a(n) kid", C.describeAge(12));
14+
assertEquals("You're a(n) teenager", C.describeAge(13));
15+
assertEquals("You're a(n) teenager", C.describeAge(14));
16+
assertEquals("You're a(n) teenager", C.describeAge(15));
17+
assertEquals("You're a(n) teenager", C.describeAge(16));
18+
assertEquals("You're a(n) teenager", C.describeAge(17));
19+
assertEquals("You're a(n) adult", C.describeAge(18));
20+
assertEquals("You're a(n) adult", C.describeAge(19));
21+
assertEquals("You're a(n) adult", C.describeAge(63));
22+
assertEquals("You're a(n) adult", C.describeAge(64));
23+
assertEquals("You're a(n) elderly", C.describeAge(65));
24+
assertEquals("You're a(n) elderly", C.describeAge(66));
25+
assertEquals("You're a(n) elderly", C.describeAge(100));
26+
}
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /