Hi all,
Struggling with a concept or at least the example below from “https://www.tutorialsteacher.com/javascript/javascript-function” which was 1 of the reading assignments on the course.
I am struggling to understand the flow of parameters…I mean I get for example where it states…document.getElementById(“p1”).innerHTML = triple(2);
From “var triple = multiple(3);” I guess we are setting x=3, but where or how are we defining what “y” needs to be?
The result of “triple(2)” is 6, and I can guess how it comes to this, but its really bugging me I can seem to logical follow the numbers through.
Any help much appreciated.
Jason
Demo: Function returing a function
<script>
function multiple(x) {
function fn(y)
{
return x * y;
}
return fn;
}
var triple = multiple(3);
document.getElementById("p1").innerHTML = triple(2);
document.getElementById("p2").innerHTML = triple(30);
</script>
</body>