0
function calculateAmount(p,r,t)
{
  function calculateSimpleInterest()
  {
    return (p*r*t)/100;
  }
  return p+calculateSimpleInterest(p,r,t);
}
console.log(calculateAmount(1000,10,2));

hey folks the above code executed successfully, but a newbie like me it still create a a lot of confusion.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 3
    The parameters passed to the outer function are visible and available for use by the inner function. That's how JavaScript works. It's called **lexical scope**. – Pointy May 05 '20 at 16:57
  • 1
    please add the problem or confusion to the question. – anees May 05 '20 at 16:57
  • 3
    There's no need to pass the parameters to the inner function for that reason; the inner function ignores its argument list. – Pointy May 05 '20 at 16:58
  • calculateSimpleInterest has acces to the perameters of calculateAmount, – Ace May 05 '20 at 16:58
  • 2
    Does this answer your question? [How do JavaScript closures work?](https://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – Wiktor Zychla May 05 '20 at 16:59
  • `return p+calculateSimpleInterest(p,r,t);` == `return p+calculateSimpleInterest("foo", "bar", "quux");` == `return p+calculateSimpleInterest("hello");` == `return p+calculateSimpleInterest();` Those arguments would be ignored. They are irrelevant. – VLAZ May 05 '20 at 17:03
  • thanks all of you for your valuable input. – Shubham Singh May 05 '20 at 17:04

0 Answers0