Errors 10 exercises
Problem

Fixing "X Is Of Type Unknown"

In this exercise, we're working with a function named somethingDangerous which uses a conditional statement based on Math.random(). If the value from Math.random() exceeds 0.5, the function will throw an error, leading to a roughly 50% chance of failure:

const somethingDangerous
Loading exercise

Transcript

00:00 In this exercise, I have a function called somethingdangerous. And somethingdangerous, what it does is it says, if math.random is greater than 0.5, it throws an error. This means there's an approximate, like it's gonna fail about 50% of the time. And so I wrap it in a try-catch.

00:17 And that try-catch here, this error, is of type unknown. And so the error I get here is error is of type unknown. Your job is to work out why this error is occurring and what I can do in terms of this catch to make sure that I actually can console log the message properly if this error is what I think it is.

00:37 So there are a few solutions here. You might need to use some instance of checks or you might need to do a type assertion. There are a few ways of getting around this. Good luck.