Calculation with Jquery

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












(function ($) 

"use strict";

$(document).ready(function ()

$('.bookinginput').on('change', function()

var adults = $('#t_adults_qty').val(),
child = $('#t_children_qty').val(),
pickup = $('#pick_up_list').val(),
priceBase = priceBooking.adult * adults,
priceChild = priceBooking.child * child;

var price = priceBase + priceChild

$('#price_total').val(price);
)

);
(jQuery));


I will explain my code a bit, basically it is a small mathematical calculation based on the value of the inputs.



I have a json with the variable priceBooking, which has two values (Adult and Child), and I have several inputs with the numbers of the (adults and children).



I simply calculate the number of (Adults for the Adult price) the same with the children.



and the result I put it in the value of another hidden input (#price_total)



I would like to simplify this code or improve it. I want to be able to work with variables globally and not have to duplicate code.



Any suggestions?







share|improve this question

















  • 2




    Please see What to do when someone answers. Do not change the code in the question after receiving an answer. Incorporating advice from an answer into the question violates the question-and-answer nature of this site.
    – Malachi♦
    Apr 17 at 21:07

















up vote
1
down vote

favorite












(function ($) 

"use strict";

$(document).ready(function ()

$('.bookinginput').on('change', function()

var adults = $('#t_adults_qty').val(),
child = $('#t_children_qty').val(),
pickup = $('#pick_up_list').val(),
priceBase = priceBooking.adult * adults,
priceChild = priceBooking.child * child;

var price = priceBase + priceChild

$('#price_total').val(price);
)

);
(jQuery));


I will explain my code a bit, basically it is a small mathematical calculation based on the value of the inputs.



I have a json with the variable priceBooking, which has two values (Adult and Child), and I have several inputs with the numbers of the (adults and children).



I simply calculate the number of (Adults for the Adult price) the same with the children.



and the result I put it in the value of another hidden input (#price_total)



I would like to simplify this code or improve it. I want to be able to work with variables globally and not have to duplicate code.



Any suggestions?







share|improve this question

















  • 2




    Please see What to do when someone answers. Do not change the code in the question after receiving an answer. Incorporating advice from an answer into the question violates the question-and-answer nature of this site.
    – Malachi♦
    Apr 17 at 21:07













up vote
1
down vote

favorite









up vote
1
down vote

favorite











(function ($) 

"use strict";

$(document).ready(function ()

$('.bookinginput').on('change', function()

var adults = $('#t_adults_qty').val(),
child = $('#t_children_qty').val(),
pickup = $('#pick_up_list').val(),
priceBase = priceBooking.adult * adults,
priceChild = priceBooking.child * child;

var price = priceBase + priceChild

$('#price_total').val(price);
)

);
(jQuery));


I will explain my code a bit, basically it is a small mathematical calculation based on the value of the inputs.



I have a json with the variable priceBooking, which has two values (Adult and Child), and I have several inputs with the numbers of the (adults and children).



I simply calculate the number of (Adults for the Adult price) the same with the children.



and the result I put it in the value of another hidden input (#price_total)



I would like to simplify this code or improve it. I want to be able to work with variables globally and not have to duplicate code.



Any suggestions?







share|improve this question













(function ($) 

"use strict";

$(document).ready(function ()

$('.bookinginput').on('change', function()

var adults = $('#t_adults_qty').val(),
child = $('#t_children_qty').val(),
pickup = $('#pick_up_list').val(),
priceBase = priceBooking.adult * adults,
priceChild = priceBooking.child * child;

var price = priceBase + priceChild

$('#price_total').val(price);
)

);
(jQuery));


I will explain my code a bit, basically it is a small mathematical calculation based on the value of the inputs.



I have a json with the variable priceBooking, which has two values (Adult and Child), and I have several inputs with the numbers of the (adults and children).



I simply calculate the number of (Adults for the Adult price) the same with the children.



and the result I put it in the value of another hidden input (#price_total)



I would like to simplify this code or improve it. I want to be able to work with variables globally and not have to duplicate code.



Any suggestions?









share|improve this question












share|improve this question




share|improve this question








edited Apr 17 at 21:09









Malachi♦

25.3k769173




25.3k769173









asked Apr 17 at 18:51









Juan David

83




83







  • 2




    Please see What to do when someone answers. Do not change the code in the question after receiving an answer. Incorporating advice from an answer into the question violates the question-and-answer nature of this site.
    – Malachi♦
    Apr 17 at 21:07













  • 2




    Please see What to do when someone answers. Do not change the code in the question after receiving an answer. Incorporating advice from an answer into the question violates the question-and-answer nature of this site.
    – Malachi♦
    Apr 17 at 21:07








2




2




Please see What to do when someone answers. Do not change the code in the question after receiving an answer. Incorporating advice from an answer into the question violates the question-and-answer nature of this site.
– Malachi♦
Apr 17 at 21:07





Please see What to do when someone answers. Do not change the code in the question after receiving an answer. Incorporating advice from an answer into the question violates the question-and-answer nature of this site.
– Malachi♦
Apr 17 at 21:07











2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










[1] Try to separate responsibilities.

Functions are things that can be (mostly) separated in three parts:



Input -> Processing -> Output



If you can, you should try to write functions that follow this logic. If one day someone need to change from where the data comes its easy to what needs to be changed and where its is used;



[2] Make clear of what type is each variable

In JavaScript you don't need to specify the type of each variable so its the job of the programmer figure out what the type is.



When you see a variable called adults, you imagine that this variable hold a list of adults, but that is not the case in your code so you should name it as something like number_of_adults or adults_quantity to make its content obvious.



So following my suggestions your code would be something like;



(function ($) 
"use strict";
$(document).ready(function ()
$('.bookinginput').on('change', function()

// "Input" <-- without the commentary, it here just to exemplify
var number_of_adults = $('#t_adults_qty').val();
var number_of_children = $('#t_children_qty').val();
var pickup = $('#pick_up_list').val();

// "Processing"
var priceBase = priceBooking.adult * number_of_adults,
var priceChild = priceBooking.child * number_of_children;
var price = priceBase + priceChild

// "Output"
$('#price_total').val(price);
)

);
(jQuery));





share|improve this answer




























    up vote
    2
    down vote













    pickup is defined, but never used.



    You should terminate your statements with semicolons consistently.






    share|improve this answer





















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      );
      );
      , "mathjax-editing");

      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: "196"
      ;
      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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192317%2fcalculation-with-jquery%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      [1] Try to separate responsibilities.

      Functions are things that can be (mostly) separated in three parts:



      Input -> Processing -> Output



      If you can, you should try to write functions that follow this logic. If one day someone need to change from where the data comes its easy to what needs to be changed and where its is used;



      [2] Make clear of what type is each variable

      In JavaScript you don't need to specify the type of each variable so its the job of the programmer figure out what the type is.



      When you see a variable called adults, you imagine that this variable hold a list of adults, but that is not the case in your code so you should name it as something like number_of_adults or adults_quantity to make its content obvious.



      So following my suggestions your code would be something like;



      (function ($) 
      "use strict";
      $(document).ready(function ()
      $('.bookinginput').on('change', function()

      // "Input" <-- without the commentary, it here just to exemplify
      var number_of_adults = $('#t_adults_qty').val();
      var number_of_children = $('#t_children_qty').val();
      var pickup = $('#pick_up_list').val();

      // "Processing"
      var priceBase = priceBooking.adult * number_of_adults,
      var priceChild = priceBooking.child * number_of_children;
      var price = priceBase + priceChild

      // "Output"
      $('#price_total').val(price);
      )

      );
      (jQuery));





      share|improve this answer

























        up vote
        2
        down vote



        accepted










        [1] Try to separate responsibilities.

        Functions are things that can be (mostly) separated in three parts:



        Input -> Processing -> Output



        If you can, you should try to write functions that follow this logic. If one day someone need to change from where the data comes its easy to what needs to be changed and where its is used;



        [2] Make clear of what type is each variable

        In JavaScript you don't need to specify the type of each variable so its the job of the programmer figure out what the type is.



        When you see a variable called adults, you imagine that this variable hold a list of adults, but that is not the case in your code so you should name it as something like number_of_adults or adults_quantity to make its content obvious.



        So following my suggestions your code would be something like;



        (function ($) 
        "use strict";
        $(document).ready(function ()
        $('.bookinginput').on('change', function()

        // "Input" <-- without the commentary, it here just to exemplify
        var number_of_adults = $('#t_adults_qty').val();
        var number_of_children = $('#t_children_qty').val();
        var pickup = $('#pick_up_list').val();

        // "Processing"
        var priceBase = priceBooking.adult * number_of_adults,
        var priceChild = priceBooking.child * number_of_children;
        var price = priceBase + priceChild

        // "Output"
        $('#price_total').val(price);
        )

        );
        (jQuery));





        share|improve this answer























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          [1] Try to separate responsibilities.

          Functions are things that can be (mostly) separated in three parts:



          Input -> Processing -> Output



          If you can, you should try to write functions that follow this logic. If one day someone need to change from where the data comes its easy to what needs to be changed and where its is used;



          [2] Make clear of what type is each variable

          In JavaScript you don't need to specify the type of each variable so its the job of the programmer figure out what the type is.



          When you see a variable called adults, you imagine that this variable hold a list of adults, but that is not the case in your code so you should name it as something like number_of_adults or adults_quantity to make its content obvious.



          So following my suggestions your code would be something like;



          (function ($) 
          "use strict";
          $(document).ready(function ()
          $('.bookinginput').on('change', function()

          // "Input" <-- without the commentary, it here just to exemplify
          var number_of_adults = $('#t_adults_qty').val();
          var number_of_children = $('#t_children_qty').val();
          var pickup = $('#pick_up_list').val();

          // "Processing"
          var priceBase = priceBooking.adult * number_of_adults,
          var priceChild = priceBooking.child * number_of_children;
          var price = priceBase + priceChild

          // "Output"
          $('#price_total').val(price);
          )

          );
          (jQuery));





          share|improve this answer













          [1] Try to separate responsibilities.

          Functions are things that can be (mostly) separated in three parts:



          Input -> Processing -> Output



          If you can, you should try to write functions that follow this logic. If one day someone need to change from where the data comes its easy to what needs to be changed and where its is used;



          [2] Make clear of what type is each variable

          In JavaScript you don't need to specify the type of each variable so its the job of the programmer figure out what the type is.



          When you see a variable called adults, you imagine that this variable hold a list of adults, but that is not the case in your code so you should name it as something like number_of_adults or adults_quantity to make its content obvious.



          So following my suggestions your code would be something like;



          (function ($) 
          "use strict";
          $(document).ready(function ()
          $('.bookinginput').on('change', function()

          // "Input" <-- without the commentary, it here just to exemplify
          var number_of_adults = $('#t_adults_qty').val();
          var number_of_children = $('#t_children_qty').val();
          var pickup = $('#pick_up_list').val();

          // "Processing"
          var priceBase = priceBooking.adult * number_of_adults,
          var priceChild = priceBooking.child * number_of_children;
          var price = priceBase + priceChild

          // "Output"
          $('#price_total').val(price);
          )

          );
          (jQuery));






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Apr 18 at 12:52









          Josenberg

          43827




          43827






















              up vote
              2
              down vote













              pickup is defined, but never used.



              You should terminate your statements with semicolons consistently.






              share|improve this answer

























                up vote
                2
                down vote













                pickup is defined, but never used.



                You should terminate your statements with semicolons consistently.






                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  pickup is defined, but never used.



                  You should terminate your statements with semicolons consistently.






                  share|improve this answer













                  pickup is defined, but never used.



                  You should terminate your statements with semicolons consistently.







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Apr 17 at 19:17









                  200_success

                  123k14142399




                  123k14142399






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192317%2fcalculation-with-jquery%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Chat program with C++ and SFML

                      Function to Return a JSON Like Objects Using VBA Collections and Arrays

                      Will my employers contract hold up in court?