I'm not quite sure what the exact bounds of your problem are, but you can use a regular expression to check for digits:
var x = prompt("Enter any characters");
if (x && x.match(/\d/)) {
// there are digits in the string
}
or written a different way (either is valid):
var x = prompt("Enter any characters");
if (x && /\d/.test(x)) {
// there are digits in the string
}
Note, this is also making sure that x
is non-null and not empty before attempting to use it as a string because if the user cancels the prompt, it will return null
.
\d
in a regular expression means to match any digit
You can read about regular expressions in javascript here and about the string methods that use them here.
I'm still guessing as to what the real bounds of your problem is, but without regex and using only the functions you mention in your comments, you can do this:
var digits = "0123456789";
// get string from user
var str = prompt("Enter any characters");
// check each digit one at a time
if (str) {
for (var i = 0; i < digits.length; i++) {
// see if the next digit is in the string
if (str.indexOf(digits.charAt(i)) {
// found a digit
}
}
}