0

In my code below, in the line ipClassRangeTop[3] = ipClassRangeBottom[3] + subNetCount - 1; it seems that the subNetCount is also added to the ipClassRangeBottom[3] as well. This is not the result I was expecting. I just want the ipClassRangeTop[3] to have that value.

Could someone people let me know what I may be doing wrong. I am very new to any type of coding so any help would be very appreciated.

var ipAddress = "172.16.1.1";
var subNetCount = 1 << 6;
var ipClassRangeTop = new Array();
var ipClassRangeBottom = new Array();
var classMaskDec = 65536;
var loopCount = 3; //classMaskDec/subNetCount;

//console.log(ipAddress, "ip address");
ipAddress = ipAddress.split(".");
ipClassRangeBottom = ipAddress;
ipClassRangeBottom[2] = 0;
ipClassRangeBottom[3] = 0;
ipClassRangeTop = ipClassRangeBottom;
//console.log(ipAddress, "ip address");

while (loopCount > 0 ) {
//console.log(i++);
console.log(ipClassRangeBottom, "1st bottom in loop");

ipClassRangeTop[3] = ipClassRangeBottom[3] + subNetCount - 1;
console.log(ipClassRangeBottom, "after top [3] assign");

if (ipClassRangeTop[3] > 255) {
    ipClassRangeTop[2] = ipClassRangeTop[2] + 1;
    ipClassRangeTop[3] = subNetCount - 1;
    console.log("IF RAN");
}

console.log(loopCount);
console.log(ipClassRangeTop);
console.log(ipClassRangeBottom);

ipClassRangeBottom[3] = ipClassRangeBottom[3] + subNetCount;
console.log(ipClassRangeBottom);
loopCount--;
}

console.log(ipAddress, "ip address");
console.log(ipClassRangeTop);
console.log(ipClassRangeBottom);

Thanks, Mark

1 Answer 1

4

Assigning a reference to an array from one variable to another does not make a copy of the array (in JavaScript). That is:

var a = [1, 2, 3];
var b = a;
b[0] = "Hello World!";
alert(a[0]); // alerts "Hello World!"

If you want to make a real copy of an array:

var a = [1, 2, 3];
var b = a.slice(0);
b[0] = "Hello World!";
alert(a[0]); // alerts "1"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.