My code will check if the variable myString
has 20 running numbers. I'm new to JavaScript and I'm wondering if there is a simpler way to do this. How can I shorten my code and possibly make it more efficient?
var myString = "tuxwuhkocx14789470019215498263ljmgwsxvne";
var runningNumbers = 0;
for (i = 0; i < myString.length; i++) {
var character = myString.charAt(i);
if (!isNaN(character)) {
// If character is a number:
runningNumbers = runningNumbers + 1;
} else {
// If character is NOT a number:
runningNumbers = 0;
}
if (runningNumbers == 20) {
console.log("myString has 20 running numbers.");
}
}
Output:
myString has 20 running numbers.
10
,-1
and0.5
might also be considered numbers. \$\endgroup\$