In: Computer Science
Write a JavaScript script that has subprograms nested three deep and in which each nested subprogram references variables defined in all of its enclosing subprograms
Here is the program that can help you:
<script>
function ParentFunction() {
var parentX = 5;
console.log(parentX);
function subChild1() {
var subChild1Y = parentX + 15;
console.log(subChild1Y);
function subChild2() {
var subChild2Z = (subChild1Y + parentX) * 2;
console.log(subChild2Z);
function finalSubChild() {
var final = parentX + subChild1Y + subChild2Z;
console.log(final);
alert('Alert From Deepest Nested Function\nParentX = '+ parentX +'\nsubChildY = '+ subChild1Y +'\nsubChildZ = '+ subChild2Z +'\nadded together = '+final);
}
finalSubChild();
}
subChild2();
}
subChild1();
}ParentFunction();
</script>
Few screenshots that can help you even better:
I hope this will help you. Do upvote if it helps you. Thanks.