Skip to content
Advertisement

Using already created functions inside a new Javascript function

I’m beginner, so this question might be silly. I had to do 4 Javascript Function, I will put them below

    function Square(a) {
        b=a*a;
        return b;
     }

    //2.Functia Half//
    function Half(a) {
        b=a/2;
        return b;

    }

    //3.Functia Percent//
    function Percent(a,b) {
        procent=a/b*100;
        return procent + "%";

    }

    //4.Functia Area//
    function Area(a) {
        pi=3.14;
        circlearea=a*a*pi;
        final=circlearea.toFixed(0);
        return final;
    }

Now i have to create the last function, which does:

Applies Half function on the parameter I’ve given to the new function(a), stores is in a new variable(result), result becomes parameter for the function square, the result will be stored again in a new variable(resultSquare), resultSquare will be used in Area function and stored in a new variable(resultArea). In the end, I need percent function, to find out the percent of resultArea from resultSquare. I tried like this, but no luck:

        function new function{
        result=Half(a);
        return result;
        resultSquare=Square(result);
        return resultSquare;
      }

It just returns result variable and does nothing else. Can you help? Thank you in advance.

Advertisement

Answer

The result exits the function – so anything after a return statement will not be run. You need to return the last item:

function newFunction(a) {
    var result = Half(a);
    var resultSquare = Square(a);
    var resultArea = Area(a);
    return Percent(resultSquare, resultArea);
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement