Hone logo
Hone
Problems

Robust Integer Parsing in JavaScript

Parsing integers from strings is a fundamental task in many programming scenarios, from user input validation to data processing. This challenge asks you to implement a function that reliably parses an integer from a string, handling various edge cases and potential errors gracefully. A robust solution is crucial for building reliable applications.

Problem Description

You are tasked with creating a JavaScript function called parseInteger that takes a string as input and returns its integer representation. The function should adhere to the following requirements:

  • Successful Parsing: If the input string represents a valid integer (e.g., "123", "-45", "0"), the function should return the integer value as a number.
  • Leading/Trailing Whitespace: The function should correctly handle strings with leading and trailing whitespace (e.g., " 123 " should return 123).
  • Sign Handling: The function should correctly handle positive and negative signs at the beginning of the string (e.g., "+123", "-45").
  • Invalid Characters: If the string contains characters other than digits, a leading sign (+ or -), or whitespace, the function should return NaN (Not a Number).
  • Empty String: If the input string is empty, the function should return NaN.
  • Overflow/Underflow: While JavaScript numbers can represent very large integers, consider that extremely large or small numbers might lead to precision issues. The function does not need to explicitly handle overflow/underflow beyond what JavaScript's number type natively provides.

Examples

Example 1:

Input: "123"
Output: 123
Explanation: The string represents a positive integer.

Example 2:

Input: "  -45  "
Output: -45
Explanation: The string contains leading and trailing whitespace and a negative sign, but still represents a valid integer.

Example 3:

Input: "+123"
Output: 123
Explanation: The string starts with a positive sign.

Example 4:

Input: "abc123"
Output: NaN
Explanation: The string contains non-numeric characters.

Example 5:

Input: ""
Output: NaN
Explanation: The input string is empty.

Example 6:

Input: "123abc"
Output: NaN
Explanation: The string contains non-numeric characters after the digits.

Constraints

  • The input will always be a string.
  • The string may contain any ASCII characters.
  • The function should be efficient enough to handle strings of reasonable length (up to 1000 characters). Performance is not the primary focus, but avoid excessively inefficient algorithms.

Notes

  • Consider using regular expressions or string manipulation methods to efficiently parse the string.
  • Remember to handle edge cases carefully to ensure the function behaves as expected in all scenarios.
  • Think about how to validate the input string before attempting to convert it to an integer.
  • JavaScript's built-in parseInt() function can be helpful, but be mindful of its behavior with non-numeric characters and its potential to return unexpected results. The goal is to implement a more robust parsing solution.
Loading editor...
javascript