Previous episode: 16. Simplify Code with When Expressions
Next episode: 18. Conclusion
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
In this challenge, you are going to fortify your knowledge of when expressions. For this, you have to use a when expression to return in which century a year of your choosing is. You have to cover at least three past centuries, and for all the cases, return a pre-defined message. Then you print out the message. That's it. Now pause the video and try to solve the challenge. Then when you're done, hit the play button and check my solution (bouncy music) I'll start off by choosing a year. Then create a parameterized when statement. This allows you to match the year to values easily. Add the following cases to match the centuries. Because we're currently in 2022, you don't have to check the future. Now store the results of this when in a value like so. And finally, print it out. Run the project and you should see the message printed out. Yeah, you used range checks for the value and an else case to cover all the years or range spans you didn't cover. You also used the when as an expression to return a value and store it in the constant you printed out. Good job.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.