isNumbersOnly
Prompt
Write a function called isNumbersOnly
that takes a string as input and returns true if the string contains only numbers, and false otherwise.
Example
console.log(isNumbersOnly('')); //false
console.log(isNumbersOnly('abcd')); //false
console.log(isNumbersOnly('abcd123')); //false
console.log(isNumbersOnly('123 ')); //false
console.log(isNumbersOnly('12340')); //true
Playground
Begin by checking if there are any spaces at the beginning or the end of the input. You can do this by comparing the trimmed version of the input with the original input. If they are not the same, what should the function return?
Next, consider spaces in your input. A string with numbers and spaces isn't "numbers only." You can use the trim()
method to check for spaces at the beginning and end, but what about spaces in the middle? How would you handle this?
For checking if each character is a digit, you have several options. One approach is to use character codes - digits 0-9 have ASCII codes from 48 to 57. You could loop through each character and check if its code falls within this range.
Another approach is to use regular expressions. JavaScript has a regex pattern ^\d+$
that matches strings containing only digits. The ^
means start of string, \d+
means one or more digits, and $
means end of string. How would you implement this?
Solution
Explanation
Let's break down our solution step by step in a way that's easy to understand:
Handling the empty string
First, we check if the input is an empty string. This is a special case we need to handle right away:
if (input === '') {
return false;
}
An empty string doesn't contain any numbers, so we return false
. This takes care of our first test case.
Checking for spaces
Next, we need to make sure there are no spaces in our input:
if (input.trim() !== input) {
return false;
}
This clever comparison checks if removing spaces from the beginning and end of the string changes it. If it does change, that means there were spaces, and we return false
. This handles our fourth test case where we have "123 "
.
Checking each character
Now comes the main part of our solution - checking if every character is a digit:
for (let i = 0; i < input.length; i++) {
const charCode = input.charCodeAt(i);
// ASCII codes for digits 0-9 are 48-57
if (charCode < 48 || charCode > 57) {
return false;
}
}
Here's what's happening:
- We loop through each character in the string
- For each character, we get its ASCII code using
charCodeAt(i)
- In ASCII, the digits 0-9 have codes 48 through 57
- If we find any character with a code outside this range, it's not a digit, so we return
false
This approach is very precise because we're checking the exact numeric representation of each character.
The final verdict
If we've made it through all our checks without returning false
, that means every character is a digit:
return true;
The character code approach is particularly nice because it works with strings that have leading zeros like "01234". If we had tried to convert the whole string to a number first, we might have lost those leading zeros.
Alternative Approach
You could also solve this using regular expressions:
const isNumbersOnly = (input) => {
return /^\d+$/.test(input);
}
The regex ^\d+$
checks if the string starts with a digit (^
), has one or more digits (\d+
), and ends with a digit ($
). This is more concise but might be harder to understand if you're not familiar with regex.