题目要求:
countIP('10.0.0.0','10.0.0.50') //50
方式1:
function ipsBetween(start, end) {
const ipLen = 4;
const step = 256;
let startNum = 0;
let startList = start.split('.');
for (let i = ipLen - 1; i >= 0; i--) {
startNum += startList[i] * Math.pow(step, ipLen - i - 1);
}
let endNum = 0;
let endList = end.split('.');
for (let i = ipLen - 1; i >= 0; i--) {
endNum += endList[i] * Math.pow(step, ipLen - i - 1);
}
return endNum - startNum;
}
方式2:
function ipsBetween(start, end){
let startNum=start.split('.').reduce((prev,cur,inx)=>prev+cur*Math.pow(256,3-inx),0);
let endNum=end.split('.').reduce((prev, cur, inx)=>prev+cur*Math. pow(256,3-inx),0);
return endNum-startNum;
}
console.log(ipsBetween('10.0.0.0','10.0.0.50'))