Call a function from an if-statement inside a function










1















I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'. Could you please point me in the right direction?



Function 1:



function printRange(rangeStart, rangeStop) 
let summa = ;

for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);

let result2 = summa.join();
return result2;



Function 2:



function printRangeReversed(rangeStart, rangeStop) 
let summa = ;

for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);

let result3 = summa.join();
return result3;



Function 3:



function printAnyRange(rangeStart, rangeStop) 

if (rangeStart < rangeStop)
printRange(rangeStart, rangeStop);
else
printRangeReversed(rangeStart, rangeStop);

return;



Calling the function: printAnyRange(21, 45);



As I said it all looks logical to me but I guess it isn't since I get the 'undefined'.



Expected result should be: "21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45"



Regards.










share|improve this question
























  • Your return statement is not followed by an expression, so the function returns undefined. Your other functions return values, but when your main function calls them it does not save the values returned.

    – Pointy
    Nov 16 '18 at 0:48












  • a) you don't return anything from printAnyRange and b) you dont' even "capture" the values returned from the other functions ... so, no surprise you get undefined at all

    – Bravo
    Nov 16 '18 at 0:49











  • "I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'." All of this is correct and as expected. What is your question?

    – melpomene
    Nov 16 '18 at 0:51











  • @melpomene Sorry, added the expected result now.

    – Thomas Bengtsson
    Nov 16 '18 at 0:55















1















I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'. Could you please point me in the right direction?



Function 1:



function printRange(rangeStart, rangeStop) 
let summa = ;

for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);

let result2 = summa.join();
return result2;



Function 2:



function printRangeReversed(rangeStart, rangeStop) 
let summa = ;

for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);

let result3 = summa.join();
return result3;



Function 3:



function printAnyRange(rangeStart, rangeStop) 

if (rangeStart < rangeStop)
printRange(rangeStart, rangeStop);
else
printRangeReversed(rangeStart, rangeStop);

return;



Calling the function: printAnyRange(21, 45);



As I said it all looks logical to me but I guess it isn't since I get the 'undefined'.



Expected result should be: "21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45"



Regards.










share|improve this question
























  • Your return statement is not followed by an expression, so the function returns undefined. Your other functions return values, but when your main function calls them it does not save the values returned.

    – Pointy
    Nov 16 '18 at 0:48












  • a) you don't return anything from printAnyRange and b) you dont' even "capture" the values returned from the other functions ... so, no surprise you get undefined at all

    – Bravo
    Nov 16 '18 at 0:49











  • "I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'." All of this is correct and as expected. What is your question?

    – melpomene
    Nov 16 '18 at 0:51











  • @melpomene Sorry, added the expected result now.

    – Thomas Bengtsson
    Nov 16 '18 at 0:55













1












1








1








I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'. Could you please point me in the right direction?



Function 1:



function printRange(rangeStart, rangeStop) 
let summa = ;

for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);

let result2 = summa.join();
return result2;



Function 2:



function printRangeReversed(rangeStart, rangeStop) 
let summa = ;

for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);

let result3 = summa.join();
return result3;



Function 3:



function printAnyRange(rangeStart, rangeStop) 

if (rangeStart < rangeStop)
printRange(rangeStart, rangeStop);
else
printRangeReversed(rangeStart, rangeStop);

return;



Calling the function: printAnyRange(21, 45);



As I said it all looks logical to me but I guess it isn't since I get the 'undefined'.



Expected result should be: "21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45"



Regards.










share|improve this question
















I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'. Could you please point me in the right direction?



Function 1:



function printRange(rangeStart, rangeStop) 
let summa = ;

for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);

let result2 = summa.join();
return result2;



Function 2:



function printRangeReversed(rangeStart, rangeStop) 
let summa = ;

for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);

let result3 = summa.join();
return result3;



Function 3:



function printAnyRange(rangeStart, rangeStop) 

if (rangeStart < rangeStop)
printRange(rangeStart, rangeStop);
else
printRangeReversed(rangeStart, rangeStop);

return;



Calling the function: printAnyRange(21, 45);



As I said it all looks logical to me but I guess it isn't since I get the 'undefined'.



Expected result should be: "21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45"



Regards.







javascript function if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 4:45









Jack Bashford

11.7k31846




11.7k31846










asked Nov 16 '18 at 0:46









Thomas BengtssonThomas Bengtsson

18613




18613












  • Your return statement is not followed by an expression, so the function returns undefined. Your other functions return values, but when your main function calls them it does not save the values returned.

    – Pointy
    Nov 16 '18 at 0:48












  • a) you don't return anything from printAnyRange and b) you dont' even "capture" the values returned from the other functions ... so, no surprise you get undefined at all

    – Bravo
    Nov 16 '18 at 0:49











  • "I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'." All of this is correct and as expected. What is your question?

    – melpomene
    Nov 16 '18 at 0:51











  • @melpomene Sorry, added the expected result now.

    – Thomas Bengtsson
    Nov 16 '18 at 0:55

















  • Your return statement is not followed by an expression, so the function returns undefined. Your other functions return values, but when your main function calls them it does not save the values returned.

    – Pointy
    Nov 16 '18 at 0:48












  • a) you don't return anything from printAnyRange and b) you dont' even "capture" the values returned from the other functions ... so, no surprise you get undefined at all

    – Bravo
    Nov 16 '18 at 0:49











  • "I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'." All of this is correct and as expected. What is your question?

    – melpomene
    Nov 16 '18 at 0:51











  • @melpomene Sorry, added the expected result now.

    – Thomas Bengtsson
    Nov 16 '18 at 0:55
















Your return statement is not followed by an expression, so the function returns undefined. Your other functions return values, but when your main function calls them it does not save the values returned.

– Pointy
Nov 16 '18 at 0:48






Your return statement is not followed by an expression, so the function returns undefined. Your other functions return values, but when your main function calls them it does not save the values returned.

– Pointy
Nov 16 '18 at 0:48














a) you don't return anything from printAnyRange and b) you dont' even "capture" the values returned from the other functions ... so, no surprise you get undefined at all

– Bravo
Nov 16 '18 at 0:49





a) you don't return anything from printAnyRange and b) you dont' even "capture" the values returned from the other functions ... so, no surprise you get undefined at all

– Bravo
Nov 16 '18 at 0:49













"I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'." All of this is correct and as expected. What is your question?

– melpomene
Nov 16 '18 at 0:51





"I have a function with an if-statement that in turn should call an outside function whether or not something is true. The result I get is 'undefined'." All of this is correct and as expected. What is your question?

– melpomene
Nov 16 '18 at 0:51













@melpomene Sorry, added the expected result now.

– Thomas Bengtsson
Nov 16 '18 at 0:55





@melpomene Sorry, added the expected result now.

– Thomas Bengtsson
Nov 16 '18 at 0:55












3 Answers
3






active

oldest

votes


















3














You're not returning anything. Capture the result of the functions and return that variable.






function printRange(rangeStart, rangeStop) 
let summa = ;

for (i = rangeStart; i <= rangeStop; i++)
summa.push(i);

let result2 = summa.join();
return result2;


function printRangeReversed(rangeStart, rangeStop)
let summa = ;

for (i = rangeStart; i >= rangeStop; i--)
summa.push(i);

let result3 = summa.join();
return result3;


function printAnyRange(rangeStart, rangeStop)
let result = null;

if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
else result = printRangeReversed(rangeStart, rangeStop);

return result;


console.log(printAnyRange(1,5));
console.log(printAnyRange(5,1));








share|improve this answer






























    2














    The value that is getting returned from function 1 and function 2 is not being using or stored by function 3. Try following code:



    function printAnyRange(rangeStart, rangeStop) 

    if (rangeStart < rangeStop)
    return printRange(rangeStart, rangeStop);
    else
    return printRangeReversed(rangeStart, rangeStop);


    console.log(printAnyRange(10, 15));


    this should fix the issue.






    share|improve this answer






























      1














      You need to do something with the returned values, like log them:



      function printAnyRange(rangeStart, rangeStop) 

      if (rangeStart < rangeStop)
      console.log(printRange(rangeStart, rangeStop));
      else
      console.log(printRangeReversed(rangeStart, rangeStop));

      return;



      Or return them from the main function:



      function printAnyRange(rangeStart, rangeStop) 

      if (rangeStart < rangeStop)
      result = printRange(rangeStart, rangeStop);
      else
      result = printRangeReversed(rangeStart, rangeStop);

      return result;






      share|improve this answer






















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329879%2fcall-a-function-from-an-if-statement-inside-a-function%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        You're not returning anything. Capture the result of the functions and return that variable.






        function printRange(rangeStart, rangeStop) 
        let summa = ;

        for (i = rangeStart; i <= rangeStop; i++)
        summa.push(i);

        let result2 = summa.join();
        return result2;


        function printRangeReversed(rangeStart, rangeStop)
        let summa = ;

        for (i = rangeStart; i >= rangeStop; i--)
        summa.push(i);

        let result3 = summa.join();
        return result3;


        function printAnyRange(rangeStart, rangeStop)
        let result = null;

        if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
        else result = printRangeReversed(rangeStart, rangeStop);

        return result;


        console.log(printAnyRange(1,5));
        console.log(printAnyRange(5,1));








        share|improve this answer



























          3














          You're not returning anything. Capture the result of the functions and return that variable.






          function printRange(rangeStart, rangeStop) 
          let summa = ;

          for (i = rangeStart; i <= rangeStop; i++)
          summa.push(i);

          let result2 = summa.join();
          return result2;


          function printRangeReversed(rangeStart, rangeStop)
          let summa = ;

          for (i = rangeStart; i >= rangeStop; i--)
          summa.push(i);

          let result3 = summa.join();
          return result3;


          function printAnyRange(rangeStart, rangeStop)
          let result = null;

          if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
          else result = printRangeReversed(rangeStart, rangeStop);

          return result;


          console.log(printAnyRange(1,5));
          console.log(printAnyRange(5,1));








          share|improve this answer

























            3












            3








            3







            You're not returning anything. Capture the result of the functions and return that variable.






            function printRange(rangeStart, rangeStop) 
            let summa = ;

            for (i = rangeStart; i <= rangeStop; i++)
            summa.push(i);

            let result2 = summa.join();
            return result2;


            function printRangeReversed(rangeStart, rangeStop)
            let summa = ;

            for (i = rangeStart; i >= rangeStop; i--)
            summa.push(i);

            let result3 = summa.join();
            return result3;


            function printAnyRange(rangeStart, rangeStop)
            let result = null;

            if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
            else result = printRangeReversed(rangeStart, rangeStop);

            return result;


            console.log(printAnyRange(1,5));
            console.log(printAnyRange(5,1));








            share|improve this answer













            You're not returning anything. Capture the result of the functions and return that variable.






            function printRange(rangeStart, rangeStop) 
            let summa = ;

            for (i = rangeStart; i <= rangeStop; i++)
            summa.push(i);

            let result2 = summa.join();
            return result2;


            function printRangeReversed(rangeStart, rangeStop)
            let summa = ;

            for (i = rangeStart; i >= rangeStop; i--)
            summa.push(i);

            let result3 = summa.join();
            return result3;


            function printAnyRange(rangeStart, rangeStop)
            let result = null;

            if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
            else result = printRangeReversed(rangeStart, rangeStop);

            return result;


            console.log(printAnyRange(1,5));
            console.log(printAnyRange(5,1));








            function printRange(rangeStart, rangeStop) 
            let summa = ;

            for (i = rangeStart; i <= rangeStop; i++)
            summa.push(i);

            let result2 = summa.join();
            return result2;


            function printRangeReversed(rangeStart, rangeStop)
            let summa = ;

            for (i = rangeStart; i >= rangeStop; i--)
            summa.push(i);

            let result3 = summa.join();
            return result3;


            function printAnyRange(rangeStart, rangeStop)
            let result = null;

            if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
            else result = printRangeReversed(rangeStart, rangeStop);

            return result;


            console.log(printAnyRange(1,5));
            console.log(printAnyRange(5,1));





            function printRange(rangeStart, rangeStop) 
            let summa = ;

            for (i = rangeStart; i <= rangeStop; i++)
            summa.push(i);

            let result2 = summa.join();
            return result2;


            function printRangeReversed(rangeStart, rangeStop)
            let summa = ;

            for (i = rangeStart; i >= rangeStop; i--)
            summa.push(i);

            let result3 = summa.join();
            return result3;


            function printAnyRange(rangeStart, rangeStop)
            let result = null;

            if (rangeStart < rangeStop) result = printRange(rangeStart, rangeStop);
            else result = printRangeReversed(rangeStart, rangeStop);

            return result;


            console.log(printAnyRange(1,5));
            console.log(printAnyRange(5,1));






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 '18 at 0:51









            Abana ClaraAbana Clara

            1,666920




            1,666920























                2














                The value that is getting returned from function 1 and function 2 is not being using or stored by function 3. Try following code:



                function printAnyRange(rangeStart, rangeStop) 

                if (rangeStart < rangeStop)
                return printRange(rangeStart, rangeStop);
                else
                return printRangeReversed(rangeStart, rangeStop);


                console.log(printAnyRange(10, 15));


                this should fix the issue.






                share|improve this answer



























                  2














                  The value that is getting returned from function 1 and function 2 is not being using or stored by function 3. Try following code:



                  function printAnyRange(rangeStart, rangeStop) 

                  if (rangeStart < rangeStop)
                  return printRange(rangeStart, rangeStop);
                  else
                  return printRangeReversed(rangeStart, rangeStop);


                  console.log(printAnyRange(10, 15));


                  this should fix the issue.






                  share|improve this answer

























                    2












                    2








                    2







                    The value that is getting returned from function 1 and function 2 is not being using or stored by function 3. Try following code:



                    function printAnyRange(rangeStart, rangeStop) 

                    if (rangeStart < rangeStop)
                    return printRange(rangeStart, rangeStop);
                    else
                    return printRangeReversed(rangeStart, rangeStop);


                    console.log(printAnyRange(10, 15));


                    this should fix the issue.






                    share|improve this answer













                    The value that is getting returned from function 1 and function 2 is not being using or stored by function 3. Try following code:



                    function printAnyRange(rangeStart, rangeStop) 

                    if (rangeStart < rangeStop)
                    return printRange(rangeStart, rangeStop);
                    else
                    return printRangeReversed(rangeStart, rangeStop);


                    console.log(printAnyRange(10, 15));


                    this should fix the issue.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 1:01









                    Pranay TripathiPranay Tripathi

                    542513




                    542513





















                        1














                        You need to do something with the returned values, like log them:



                        function printAnyRange(rangeStart, rangeStop) 

                        if (rangeStart < rangeStop)
                        console.log(printRange(rangeStart, rangeStop));
                        else
                        console.log(printRangeReversed(rangeStart, rangeStop));

                        return;



                        Or return them from the main function:



                        function printAnyRange(rangeStart, rangeStop) 

                        if (rangeStart < rangeStop)
                        result = printRange(rangeStart, rangeStop);
                        else
                        result = printRangeReversed(rangeStart, rangeStop);

                        return result;






                        share|improve this answer



























                          1














                          You need to do something with the returned values, like log them:



                          function printAnyRange(rangeStart, rangeStop) 

                          if (rangeStart < rangeStop)
                          console.log(printRange(rangeStart, rangeStop));
                          else
                          console.log(printRangeReversed(rangeStart, rangeStop));

                          return;



                          Or return them from the main function:



                          function printAnyRange(rangeStart, rangeStop) 

                          if (rangeStart < rangeStop)
                          result = printRange(rangeStart, rangeStop);
                          else
                          result = printRangeReversed(rangeStart, rangeStop);

                          return result;






                          share|improve this answer

























                            1












                            1








                            1







                            You need to do something with the returned values, like log them:



                            function printAnyRange(rangeStart, rangeStop) 

                            if (rangeStart < rangeStop)
                            console.log(printRange(rangeStart, rangeStop));
                            else
                            console.log(printRangeReversed(rangeStart, rangeStop));

                            return;



                            Or return them from the main function:



                            function printAnyRange(rangeStart, rangeStop) 

                            if (rangeStart < rangeStop)
                            result = printRange(rangeStart, rangeStop);
                            else
                            result = printRangeReversed(rangeStart, rangeStop);

                            return result;






                            share|improve this answer













                            You need to do something with the returned values, like log them:



                            function printAnyRange(rangeStart, rangeStop) 

                            if (rangeStart < rangeStop)
                            console.log(printRange(rangeStart, rangeStop));
                            else
                            console.log(printRangeReversed(rangeStart, rangeStop));

                            return;



                            Or return them from the main function:



                            function printAnyRange(rangeStart, rangeStop) 

                            if (rangeStart < rangeStop)
                            result = printRange(rangeStart, rangeStop);
                            else
                            result = printRangeReversed(rangeStart, rangeStop);

                            return result;







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 16 '18 at 0:51









                            Jack BashfordJack Bashford

                            11.7k31846




                            11.7k31846



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329879%2fcall-a-function-from-an-if-statement-inside-a-function%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Top Tejano songwriter Luis Silva dead of heart attack at 64

                                政党

                                天津地下鉄3号線