# Hepl with some code

• 12-09-2013, 02:12 PM
Sagitario
Hepl with some code
Hi,

I need some help with my code.

I have everything working but I intend to turn it more simple and small.

I have a form that a user choose between two dates and the choose a category. Then in javascript code calculate the total of days, check if the days are in a low, medium or high season and then go to a table with prices and then return the full price.

This is part of my code:
Code:

```var a_low_2=39;                var a_low_4=55;                var a_low_5=68;                        var a_low_7=88; var a_mid_2=63;                var a_mid_4=88;                var a_mid_5=110;                var a_mid_7=129; var a_high_2=75;        var a_high_4=110;        var a_high_5=130;                var a_high_7=173; var a1_low_2=58;        var a1_low_4=116;        var a1_low_5=145;                var a1_low_7=196; var a1_mid_2=224;        var a1_mid_4=116;        var a1_mid_5=145;                var a1_mid_7=196; var a1_high_2=58;        var a1_high_4=116;        var a1_high_5=145;                var a1_high_7=196; if (season=="low" && car_choose=="a") {         switch(total_days)         {                 case 1:        payment=a_low_2;        break;                 case 2:        payment=a_low_2;        break;                 case 3:        payment=a_low_4;        break;                 case 4:        payment=a_low_4;        break;                 case 5:        payment=a_low_5;        break;                 case 6:        payment=a_low_7;        break;                 case 7:        payment=a_low_7;        break;                 default:        payment=(a_low_7/7)*total_days;         } } if (season=="mid" && car_choose=="a") {         switch(total_days)         {                 case 1:        payment=a_mid_2;        break;                 case 2:        payment=a_mid_2;        break;                 case 3:        payment=a_mid_4;        break;                 case 4:        payment=a_mid_4;        break;                 case 5:        payment=a_mid_5;        break;                 case 6:        payment=a_mid_7;        break;                 case 7:        payment=a_mid_7;        break;                 default:        payment=(a_mid_7/7)*total_days;         } } if (season=="high" && car_choose=="a") {         switch(total_days)         {                 case 1:        payment=a_high_2;        break;                 case 2:        payment=a_high_2;        break;                 case 3:        payment=a_high_4;        break;                 case 4:        payment=a_high_4;        break;                 case 5:        payment=a_high_5;        break;                 case 6:        payment=a_high_7;        break;                 case 7:        payment=a_high_7;        break;                 default:        payment=(a_high_7/7)*total_days;         } } if (season=="low" && car_choose=="a1") {         switch(total_days)         {                 case 1:        payment=a1_low_2;        break;                 case 2:        payment=a1_low_2;        break;                 case 3:        payment=a1_low_4;        break;                 case 4:        payment=a1_low_4;        break;                 case 5:        payment=a1_low_5;        break;                 case 6:        payment=a1_low_7;        break;                 case 7:        payment=a1_low_7;        break;                 default:        payment=(a1_low_7/7)*total_days;         } } if (season=="mid" && car_choose=="a1") {         switch(total_days)         {                 case 1:        payment=a1_mid_2;        break;                 case 2:        payment=a1_mid_2;        break;                 case 3:        payment=a1_mid_4;        break;                 case 4:        payment=a1_mid_4;        break;                 case 5:        payment=a1_mid_5;        break;                 case 6:        payment=a1_mid_7;        break;                 case 7:        payment=a1_mid_7;        break;                 default:        payment=(a1_mid_7/7)*total_days;         } } if (season=="high" && car_choose=="a1") {         switch(total_days)         {                 case 1:        payment=a1_high_2;        break;                 case 2:        payment=a1_high_2;        break;                 case 3:        payment=a1_high_4;        break;                 case 4:        payment=a1_high_4;        break;                 case 5:        payment=a1_high_5;        break;                 case 6:        payment=a1_high_7;        break;                 case 7:        payment=a1_high_7;        break;                 default:        payment=(a1_high_7/7)*total_days;         } }```
And I have this from the letter "A" to "J".

I pretend to have something more simples like:

Code:

```if (season=="low") {         switch(total_days)         {                 case 1:        payment=car_choose + _low_2;        break;                 case 2:        payment=car_choose + _low_2;        break;                 case 3:        payment=car_choose + _low_4;        break;                 case 4:        payment=car_choose + _low_4;        break;                 case 5:        payment=car_choose + _low_5;        break;                 case 6:        payment=car_choose + _low_7;        break;                 case 7:        payment=car_choose + _low_7;        break;                 default:        payment=(car_choose + _low_7/7)*total_days;         } } //And the same for the other two seasons...```
But the result of this code is the name of the variable, not the value.

Can someone tell me how can I join 2 variable to turn in to one and the get the value of that variable e note the name?

• 12-09-2013, 03:02 PM
tech_soul8
At first glance there are few things you can do to make your code little shorter.

You don't have to declare all of your variables with the var keyword. You can declare multiple variables and assign them a value with the use of a comma operator which has a special meaning when used with the var keyword.

Code:

`var  a_low_2 = 39, a_low_4 = 55, a_low_5 = 68...`
also in your switch statement if the result of expression matches multiple "case" statements you can define it like this:

Code:

```switch (expression) {     case 1:     case 2:     case 3: {         //some code to execute         break;     }     default: {         //some code         break;     } }```
• 12-09-2013, 03:05 PM
tech_soul8
Quote:

Originally Posted by Sagitario

Can someone tell me how can I join 2 variable to turn in to one and the get the value of that variable e note the name?

Code:

```var a = 10; var b = 10; var c = a + b; //or shorter version var a = 10, b = 10, c = a + b;```
...but you have to be aware of type of values you are having in those variables because depending on the type of a value JavaScript performs different operations. For example if you are trying to add two values but one of them is a string literal you're end with the values being concatenated rather then added.

Code:

```var a = 10; var b = "10"; var c = a + b //evaluates to 1010 not 20```
• 12-10-2013, 08:27 AM
Sagitario
Hi tech_soul8,

Thank you for the support. The var and the switch expression help to short a little bit my code, I will implement that.

But the big part remains, I have to do a switch 3 times from A to J. This are to many switch to use.

I understand the a + b expression with the result values being concatenated or added, but this is not what I have in mind. That is what I get doing:
Code:

`payment=car_choose + _low_2; // e.g. the variable payment will be equal to "a_low_2" instead of be equal to 39`
I understand that the expression that I'm using is wrong, the problem is that I don't know the correct one.

Regards,
• 12-10-2013, 10:57 AM
Kevin2
Code:

`payment=1*car_choose +1* _low_2;`
• 12-10-2013, 11:02 AM
bionoid
If you are prepared to restructure your data, you can make it very compact:

Code:

```var         data = {                 a : [                         [39,  55,  68,  88], //low                         [63,  88, 110, 129], //mid                         [75, 110, 130, 173]  //high                 ],                 a1 : [                         [ 58, 116, 145, 196], //low                         [224, 116, 145, 196], //mid                         [ 58, 116, 145, 196]  //high                 ]         },         payment, season, car_choose, total_days; /* FETCH THE PARAMETERS FROM YOUR FORM */ season    = 'mid'; car_choose = 'a1'; total_days = 3; /* DETERMINE PAYMENT AMOUNT */ payment = data[car_choose][{low : 0, mid : 1, high : 2}[season]][[0, 0, 1, 1, 2, 3, 3][Math.min(total_days, 7) - 1]] / (total_days < 8 ? 1 : 7); alert(payment); //116```
• 12-11-2013, 08:42 AM
Sagitario
Thank you all, but the solution that bionoid have given was the one that I searching.

The only think that is not working is when is more that 8 days, the result is total_days / 7 and not (total_days / 7)*total_days

Regards,
• 12-11-2013, 08:49 AM
bionoid
Sorry, I didn't notice that... see if this works better:

Code:

```var         data = {                 a : [                         [39,  55,  68,  88], //low                         [63,  88, 110, 129], //mid                         [75, 110, 130, 173]  //high                 ],                 a1 : [                         [ 58, 116, 145, 196], //low                         [224, 116, 145, 196], //mid                         [ 58, 116, 145, 196]  //high                 ]         },         payment, season, car_choose, total_days; /* FETCH THE PARAMETERS FROM YOUR FORM */ season    = 'mid'; car_choose = 'a1'; total_days = 3; /* DETERMINE PAYMENT AMOUNT */ payment = (data[car_choose][{low : 0, mid : 1, high : 2}[season]][[0, 0, 1, 1, 2, 3, 3][Math.min(total_days, 7) - 1]] / (total_days < 8 ? 1 : 7)) * (total_days < 8 ? 1 : total_days); alert(payment); //116```
• 12-11-2013, 08:59 AM
Sagitario
Thank bionoid, you are the best!!

Now it works all perfect. My code is now more smaller and easy to work it...

Thanks again!!

Regards,

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.09887 seconds
• Memory Usage 2,406KB
• Queries Executed 11 (?)
Template Usage (21):
• (10)bbcode_code_printable
• (1)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates