0
<!doctype html>
<html>
  <head>
    <script>
      function do_something() {
        var theBody = document.getElementsByTagName("body")[0];
        var theImg = document.createElement("img");
        theImg.src = "cat.png";
        theBody.appendChild(theImg.cloneNode(true));
        var count = 0;
        for (var i = 0; i < 10; i++, count++) {
            if (i == 5) continue;
            if (count == 3) {
                count = 0;
                theBody.removeChild(theBody.lastChild);
            } else {
                theBody.appendChild(theImg.cloneNode(true));
            }
            if (i > 7) break;
        }
      }
    </script>
  </head>
  <body onload="do_something()"></body>
</html>

I am supposed to tell how many images would a modern browser display.

I have two major doubts here:

  • When i=4, what is the value of count? I think it would be 0, but don't why I am confused about it.
  • As the code tells, when count = 0, an image will be removed from the body. Does the code append an image, and then remove an image? Or, does it simply remove one image? This is the part that is confusing because nothing is said about what happens when i=3.

According to the given answer, 6 images are added in the loop, and 2 are removed. So, a total number of 5 images are displayed.

4
  • 1. Yes, it will be 4. 2. It removes the third and sixth image, making it totally 5 images, as the numbers are 0 starting. 0 - 6 is seven images, out of which two are removed leaving five images. Commented Oct 1, 2016 at 12:35
  • 1
    As a note: Beside many others this is a good situation to use the javascript debugger build into all modern browsers, and step through the code instruction by instruction. Then you exactly see what happens. Commented Oct 1, 2016 at 12:39
  • @PraveenKumar I didn't get it, count = 4 ? Doesn't it get reset to 0, once it reaches 3? Also, wouldn't the code run when i = 7? Commented Oct 1, 2016 at 12:51
  • @AyushBahuguna Sorry, when 4, it will be 0. Commented Oct 2, 2016 at 12:07

1 Answer 1

1

When i=4, what is the value of count? I think it would be 0, but don't why I am confused about it.

When i == 3, count is set to 0. At the end of the for block, count++ is executed, so by the time i == 4, count == 1

As the code tells, when count = 0, an image will be removed from the body. Does the code append an image, and then remove an image? Or, does it simply remove one image? This is the part that is confusing because nothing is said about what happens when i=3.

  • i == 0 append
  • i == 1 append
  • i == 2 append
  • i == 3 image from previous step removed
  • i == 4 append

etc.

Sign up to request clarification or add additional context in comments.

3 Comments

So, when i=3, does it add an image and then remove it, or does it remove the one that was appended when i =2?
It removes the image that was added in the previous step
@AyushBahuguna I've edited to include that. Could you accept?

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.