Add underscore _ to text string

makiz63
Kilo Contributor

I have the following script which is not working, I want to have an underscore after the 'Room" before the var_area is displayed

ie:  Room_ComptuterRoom301

if (loc1) {

desk +="Room" + "_" + 'var_area\n';

}

How do I join the symbol.

1 ACCEPTED SOLUTION

Okay, I see a few things wrong with this. First, loc1 and loc2 are booleans, not string, so the end result will be Room_true or Room_false, not Room_London or Room_NewYork.

 

But I'm still a bit confused over the checkbox thing. If there are two checkboxes, one for each city, there are four states in all: both selected, one selected, the other selected, and neither selected. It sounds like you might want to make that a drop-down choice list instead of checkboxes.

 

That having been said, I took a crack at rewriting your code to be a bit shorter, and hopefully accomplishing what you're trying to do. The caveat is that if both checkboxes are selected, it's going to stop at the first city ('London') without checking the other one. If neither checkbox is selected, it won't do anything. Here's the code:

 

function onSubmit() {
    var city = [ 'London', 'NewYork' ];
    var loc = 'Room' + (g_form.getValue('deskt') == 'perm' ? 'PM' : '') + '_';
    
    for (var i = 0; i < cities; i++) {
        if (g_form.getValue(city[i]) == 'true') {
            g_form.setValue('room', loc + city[i]);
            return;
        }
    }
    
    // If you've reached this point, no location was selected, so don't set
    // the room field value.
}

 

If the tertiary operator (?:) is confusing, all it does is evaluate the the value before the colon ('PM') if the expression to the left of the ? is true, or the value after the colon ('') if the expression to the left of the ? is false. It's just a syntactic shortcut for an if statement.

 

There are other ways to implement this, but this is flexible enough that if you add more checkboxes later, just add them to the array.

 

Hope this helps,

--Dennis R

View solution in original post

12 REPLIES 12

I'm a bit confused. You have two checkboxes, but only one selected location. What happens if the user checks both London and New York? What should selectedLocation bet set to?

Okay, I see a few things wrong with this. First, loc1 and loc2 are booleans, not string, so the end result will be Room_true or Room_false, not Room_London or Room_NewYork.

 

But I'm still a bit confused over the checkbox thing. If there are two checkboxes, one for each city, there are four states in all: both selected, one selected, the other selected, and neither selected. It sounds like you might want to make that a drop-down choice list instead of checkboxes.

 

That having been said, I took a crack at rewriting your code to be a bit shorter, and hopefully accomplishing what you're trying to do. The caveat is that if both checkboxes are selected, it's going to stop at the first city ('London') without checking the other one. If neither checkbox is selected, it won't do anything. Here's the code:

 

function onSubmit() {
    var city = [ 'London', 'NewYork' ];
    var loc = 'Room' + (g_form.getValue('deskt') == 'perm' ? 'PM' : '') + '_';
    
    for (var i = 0; i < cities; i++) {
        if (g_form.getValue(city[i]) == 'true') {
            g_form.setValue('room', loc + city[i]);
            return;
        }
    }
    
    // If you've reached this point, no location was selected, so don't set
    // the room field value.
}

 

If the tertiary operator (?:) is confusing, all it does is evaluate the the value before the colon ('PM') if the expression to the left of the ? is true, or the value after the colon ('') if the expression to the left of the ? is false. It's just a syntactic shortcut for an if statement.

 

There are other ways to implement this, but this is flexible enough that if you add more checkboxes later, just add them to the array.

 

Hope this helps,

--Dennis R

var desktype = g_form.getValue('deskt');
var loc1 = g_form.getValue('London');
var loc2 = g_form.getValue('NewYork');
var selectedLocation = '';

if(loc1 && desktype =='temp'){
selectedLocation += "Room_" + loc1 + "\n";// this should display output of Room_London

}
if (loc2 && desktype =='perm'){
selectedLocation += "RoomPM_" + loc2 +"\n" // this should display output of RoomPM_NewYork
}

g_form.setValue('Room', selectedLocation);

 

also check the length of string variable.