/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 验证IP地址
* @param IP string字符串 一个IP地址字符串
* @return string字符串
*/
function solve(IP) {
// write code here
// console.log(IP)
let arrs = [];
if (IP.includes(".")) {
arrs = IP.split(".");
if (arrs.length === 4) {
let valid = arrs.every((num) => {
if (num == "") {
return false;
}
if (num.length > 1 && num[0] == 0) {
return false;
}
let n = Number(num);
if (0 <= n && n <= 255) {
return true;
} else {
return false;
}
});
if (valid) {
return "IPv4";
} else {
return "Neither";
}
} else {
return "Neither";
}
} else {
arrs = IP.split(":");
if (arrs.length === 8) {
let valid = arrs.every((num) => {
if (num === "") {
return false;
}
if (num.length > 4) {
return false;
}
return /^[0-9A-Fa-f]+$/.test(num)
});
if (valid) {
return "IPv6";
} else {
return "Neither";
}
} else {
return "Neither";
}
}
// console.log(arrs)
}
module.exports = {
solve: solve,
};