www.webdeveloper.com
Results 1 to 9 of 9

Thread: Hepl with some code

  1. #1
    Join Date
    Dec 2013
    Posts
    4

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

    Thank you in advance.

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    247
    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. #3
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    247
    Quote Originally Posted by Sagitario View Post

    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?

    Thank you in advance.
    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. #4
    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. #5
    Join Date
    Oct 2013
    Posts
    404
    Code:
    payment=1*car_choose +1* _low_2;

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    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
    Last edited by bionoid; 12-10-2013 at 10:11 AM.
    JavaScript: Learn | Validate | Compact | bionoid

  7. #7
    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. #8
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    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
    JavaScript: Learn | Validate | Compact | bionoid

  9. #9
    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,

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles