Calculation with Jquery
Clash 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?
javascript jquery html json
add a comment |Â
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?
javascript jquery html json
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
add a comment |Â
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?
javascript jquery html json
(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?
javascript jquery html json
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
add a comment |Â
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
add a comment |Â
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));
add a comment |Â
up vote
2
down vote
pickup
is defined, but never used.
You should terminate your statements with semicolons consistently.
add a comment |Â
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));
add a comment |Â
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));
add a comment |Â
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));
[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));
answered Apr 18 at 12:52
Josenberg
43827
43827
add a comment |Â
add a comment |Â
up vote
2
down vote
pickup
is defined, but never used.
You should terminate your statements with semicolons consistently.
add a comment |Â
up vote
2
down vote
pickup
is defined, but never used.
You should terminate your statements with semicolons consistently.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
pickup
is defined, but never used.
You should terminate your statements with semicolons consistently.
pickup
is defined, but never used.
You should terminate your statements with semicolons consistently.
answered Apr 17 at 19:17
200_success
123k14142399
123k14142399
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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