DEV Community

Himanshupal0001
Himanshupal0001

Posted on

returning function and arguments !!!JS

So I'm currently practicing Js. Even though I cleared the basic concepts of Js It might be confusing when we implement into actual programming. I'll show the example below and then describe what I leaned from it. It might be not intimidating but It confuses beginners.

In this example we need to calculate time to prepare lasagna. We have a cook book with some instructions which we need to follow.

The problem is consist of two variables and three functions.

Now lets see the code

Note that the parameters in minutes

const timeTakenPerLayer = 2;
const totalTimeToCook = 40

function RemainingTime(actualTimeInOven)
{
return totalTimeToCook - actualTimeInOven; //supose 40 - 20 = 20
}

function TimeTakenPerLayer(numberOfLayers)
{
return numberOfLayers * timeTakenPerLayer; //suppose 2*2 = 4
}

function TotalTime(numberOfLayers, actualTimeInOven)
{
return TimeTakenPerLayer(numberOfLayers) + actualTImeInOven;
}


*Suppose number of layers = 1, actualTimeInOven = 5
So expected outcome must be //7

Enter fullscreen mode Exit fullscreen mode

What I learned -> What I learned from this example is that we can return a functions as well as an argument of a function. It will not give any error. Also we need to deeply analyze the problem and instead of connecting the problem to actual scenario we must also see it as mathematical problem.

Like for lasagna layers we can easily calculate minutes by calculating it to a constant like 2,4,5,7,8. But instead of this suppose it as n number of layers. That will help you to solve the program for infinite solution and more logically and also how you can store that value to variable for further use.

Let me know your views, suggestions or corrections in comments.

Top comments (0)