Hi everyone I’m having difficulty understanding the following function structures.
Could somebody walk me through it please? I’m doing it as part of my JS course.
I have tested the functions and have seen the output but I’m not sure how JS comes to that conclusion.
Thanks in advance.
// Number 1
function doit(m){
return(m+2);
}
function ask(n){
return(n+doit(n+1)+n);
}
alert(ask(2));
// Number 2
function doit(n){
return(n+1);
}
function ask(p,q){
return(p+doit(q+2)+q);
}
alert(ask(2,3));
I am not sure how to read it!
askis a function that takes one parameter and does the following+nat the end.doIt(n+1)to it.Thus we have
n + n + doIt(n+1)Now
doItis another function that takes a value and returns it’s but incremented twice.This gives us a total of
n + n + n + 1 + 2Which is3*(n+1)Example two is left as an exercise for the reader