Tuesday, August 23, 2022

Js - Do and work is one hting know why is other

 The logic of that snippet of mdn

function loop(x) {                                    

  if (x >= 10) {                                      
    return;                                           
  }                                                   
  // do stuff ->you could say something like document.write('Say'+x);
  loop(x + 1); // the recursive call
}                                                     
loop(0);                                              

Let make some annotation to the explanation, the all the comand are inside the parenthesys function let risk of our preocupation and we gonna have in the same range the

 -->'if'

-->the do stuff that we gonna replace for a command like give a job for it

-->loop(x+1);

So why we gonna receive a return if is inside the first, and you see the return don't point what will gonna be returned?

And is that let explain with the case asked by the problem loop(0), a practical example

Every turn of the code I said that add one to the x that will be 0, 0+1=1, 1+1 = 2, 3, 4, but we have an if that said till the x be equal of 10, need the less than? No what defines we increase is add and not if is less than, so after road the two other command inside the function that is the parent of all them we have the first child and inside of it we have return, so the child of the if will return what is inside the function and not exactly what the two other commands above his parent do, see the command is corrupted and isn't return that but return, and it returning what is inside the parent of the parent. Rute Bezerra de Menezes Gondim










No comments:

Post a Comment