# Thread: Hepl with some code

1. Registered User
Join Date
Dec 2013
Posts
4

## 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?

2. 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;
}
}```
Last edited by tech_soul8; 12-09-2013 at 02:10 PM.

3. 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```
Last edited by tech_soul8; 12-09-2013 at 02:09 PM.

4. Registered User
Join Date
Dec 2013
Posts
4
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,

5. Registered User
Join Date
Oct 2013
Posts
1,694
Code:
`payment=1*car_choose +1* _low_2;`

6. 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);

Last edited by bionoid; 12-10-2013 at 10:11 AM.

7. Registered User
Join Date
Dec 2013
Posts
4
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,

8. 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);

9. Registered User
Join Date
Dec 2013
Posts
4
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,