Help with break statement

Abhijit Das7
Tera Expert

Hi Everyone,

 

I have a string, when I convert this string into array. The last element of the array is empty; I want to break the for loop when element is empty. Please help me with the issue.

 

sting = John, \n Guest 1, \n Guest 2, \n

var result = ' ';

var array = string.split(' , ');
for( var i = 0 ; i < array.length ; i++){

result +=  array[i] + 'joined the session';
}

 result += "";

 Thanks

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Abhijit Das7 

try this and it worked for me

var string = "John, \n Guest 1, \n Guest 2, \n";
var result = '';

var array = string.split(', \n');
for (var i = 0; i < array.length; i++) {
    if (array[i].trim() === '') {
        break; // Break the loop if the element is empty
    }
    result += array[i].trim() + ' joined the session\n';
}

gs.info(result);

Output:

AnkurBawiskar_0-1744713538782.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Shivalika
Mega Sage

Hello @Abhijit Das7 

 

use below script:- If empty means = '' or whatever you can replace accordingly. 

sting = John, \n Guest 1, \n Guest 2, \n

var result = ' ';

var array = string.split(' , ');
for( var i = 0 ; i < array.length ; i++){

//ADD below two lines - if this is what you meant by empty
if(array[i]=='')
break;

result +=  array[i] + 'joined the session';
}

 result += "";

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Abhijit Das7 

try this and it worked for me

var string = "John, \n Guest 1, \n Guest 2, \n";
var result = '';

var array = string.split(', \n');
for (var i = 0; i < array.length; i++) {
    if (array[i].trim() === '') {
        break; // Break the loop if the element is empty
    }
    result += array[i].trim() + ' joined the session\n';
}

gs.info(result);

Output:

AnkurBawiskar_0-1744713538782.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Shree_G
Mega Sage

Hello @Abhijit Das7 ,

 

Added a pointer to count the length of array and stored with less one value.

var sting = "John, \n Guest 1, \n Guest 2, \n";

var result="";

var array = sting.split(',');
var leg = --array.length;   //storing the array length -1
for( var i = 0 ; i < leg ; i++){
result +=  array[i] + 'joined the session';
}
 gs.print(result);

 

Output:

Shree_G_0-1744713648210.png

 

Is this the desired output.

 


If this solution helped resolve your issue, please consider marking it as helpful or correct.
This will assist others in finding the solution faster and close the thread.