I think itβs very funny to come up with ways to solve this problem. Below are some of them. (All functions below assume that the argument is a single character. Go to n[0] to ensure it runs)
Method 1:
function isCharDigit(n){ return !!n.trim() && n > -1; }
Method 2:
function isCharDigit(n){ return !!n.trim() && n*0==0; }
Method 3:
function isCharDigit(n){ return !!n.trim() && !!Number(n+.1);
Method 4:
var isCharDigit = (function(){ var a = [1,1,1,1,1,1,1,1,1,1]; return function(n){ return !!a[n]
Method 5:
function isCharDigit(n){ return !!n.trim() && !isNaN(+n); }
Test line:
var str = ' 90ABcd#?:.+', char; for( char of str ) console.log( char, isCharDigit(char) );
vsync Sep 14 '15 at 19:35 2015-09-14 19:35
source share