We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In JavaScript/TypeScript, the situation is a bit different. The parseInt function does not throw an exception when the conversion fails. Instead, it returns NaN (Not-a-Number), which means we need additional validation to handle this case. Due to the challenge's restrictions, which do not allow the use of conditionals, it was not possible to submit a solution that met all the requirements.
Here is an attempt at a solution in TypeScript using parseInt for conversion, but it requires additional validation for NaN:
In Java, the solution was straightforward thanks to Integer.parseInt, which throws a NumberFormatException if the string cannot be converted to an integer. This allows us to handle the conversion error clearly and simply using a try-catch block. Here's the code:
Day 16: Exceptions - String to Integer
You are viewing a single comment's thread. Return to all comments →
Solution in JavaScript/TypeScript
In JavaScript/TypeScript, the situation is a bit different. The parseInt function does not throw an exception when the conversion fails. Instead, it returns NaN (Not-a-Number), which means we need additional validation to handle this case. Due to the challenge's restrictions, which do not allow the use of conditionals, it was not possible to submit a solution that met all the requirements. Here is an attempt at a solution in TypeScript using parseInt for conversion, but it requires additional validation for NaN:
Solution in Java
In Java, the solution was straightforward thanks to Integer.parseInt, which throws a NumberFormatException if the string cannot be converted to an integer. This allows us to handle the conversion error clearly and simply using a try-catch block. Here's the code: