0
why isnt my for loop counting as instructed???
testing ip adress ping, im sure ill figure out the "c" variable when i figure out how to make the "b" variable work, problem,is the loops working but the b variable isnt incrementing on the output, instead it just keeps printing "b0" (variables name with variables value) when i change it to the "x" variable that the "b" variable is assigned to in the loop, the output jus does the same thing only instead of "b0" now it plugs in "x0"??? im confused https://code.sololearn.com/WvxdEI2u5cYT/?ref=app
1 Answer
0
In your code:
var a= 167.192;
var b= .0;
var c= .1;
for (x=b; x<200; x++){
document.write(a+"x"+c+"<br />");
}
var a, b and c are Number types
in the for loop x is also a Number type, and it is incremented by x++, but it is never output
document.write(a+"x"+c+"<br />");
"x" in the line above is a literal String type and not the same as the var x
so the var a is implicitly converted to a String type and concatenated with "x" which results in:
"167.192x" which is then concatenated with the var c that is also implicitly converted into a String type, to result in
"167.192x0.1"
var c is equal to .1 as a Number type so the leading 0 is added to give 0.1 which has the same mathematical value.
The code below should give you the result you're looking for:
var a= '167.192.'; // change to a String type and add the '.' to the end instead of beginning of b
// b isn't really needed and can be replaced by x in the for loop
var c= '.1'; // likewise change c to a String type
for (var x = 0; x<200; x++){
document.write(a + x + c + "<br />");
}
This will output:
'167.192.XXX.1'
where XXX is a value from 0 - 199