16 lines JavaScript Solution


  • 0
    Z
    var ip4 = 1, ip6 = 1, s1 = IP.split('.'),s2 = IP.split(':');
    // ip4 = 0;
    if(s1.length != 4) ip4 = 0;
    if(s1.length == 4){
        for(var i = 0; i < 4; i++){
            if(/[^0-9]/.test(s1[i])||s1[i].length < 1||s1[i].length !=1 &&s1[i][0] == '0'|| Number.parseInt(s1[i])<0||Number.parseInt(s1[i])>255) {ip4 = 0;break;}
        }
    }
    if(ip4) return 'IPv4';
    
    if(s2.length != 8) ip6 = 0;
    if(s2.length == 8){
        for(var j = 0; j< 8; j++){
            if(s2[j].length > 4||s2[j].length < 1||/[^a-fA-F0-9]/.test(s2[j])) {ip6 = 0;break;}
        }
    }
    return (ip6)?'IPv6':'Neither';

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.