-4

To determine the steps it'll take for "y" to catch up with "z."

var z = 50;
var y = 1;
do {
  document.write(z, "-");
  z++
} while (z >= y);
do {
  document.write(y, "/");
  y = y + 2
} while (y <= z);
  • 1
    Tell me what you think that first do-while loop is doing. – Herohtar May 31 '19 at 04:03
  • The first loop increases `z` while `(z >= y)`. But `z` is already greater than `y`, and will continue to be so while you increment `z` – CertainPerformance May 31 '19 at 04:03
  • But wouldn't "y" catch up with "z" eventually, as (y=y+2) and (z=z+1)? – Coop Cooper May 31 '19 at 04:07
  • JavaScript code [is synchronous](https://stackoverflow.com/questions/2035645/when-is-javascript-synchronous) unless you are using [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). Code does not run in parallel. The second loop will not start until the first one completes, which in this case is never. – Herohtar May 31 '19 at 04:10
  • What I'm confused about is whether both the loops are running simultaneously. Don't mind my obliviousness, I began learning JS just yesterday. – Coop Cooper May 31 '19 at 04:11
  • @Herohtar Ah well, this clears my doubt. Thanks! – Coop Cooper May 31 '19 at 04:12
  • Note that you could do both the `z++` and `y = y + 2` in the same loop and use a single condition. – Herohtar May 31 '19 at 04:14

1 Answers1

0
var z = 50;
var y = 1;
function test(z,y){
    z++;
    y=y+2;
    console.log(z,y,'try');
    if(y>z){
        console.log(z,y,'you do it');
        return false;
    }else{
        test(z,y);
    }
}
test(z,y);
周清國
  • 14
  • 3