Reverse text string

inverted

Error at Assign value to variable in main.neek :x.reverse is not a function. Please refer to Help Center to make sure the activity ‘Assign value to variable’ was set up correctly. If it doesn’t help, click this link to contact support.

I am reversing a text string, but I get an error when executing it. What else could be done in this case?

1 Like

@Jose_Miguel_Almonte, the javascript method reverse() works with arrays and not with strings. So one possible solution is to split a string to create an array, using split(), then use reverse() and finally convert the array into string again by using join.

This is the workflow I have created to test it:

And this is the script I used inside the “Execute JS code” activity:

function reverseString(str) {
    // Step 1. Use the split() method to return a new array
    var splitString = str.split(""); 
    
    // Step 2. Use the reverse() method to reverse the new created array
    var reverseArray = splitString.reverse(); 
 
    // Step 3. Use the join() method to join all elements of the array into a string
    var joinArray = reverseArray.join("");
    
    //Step 4. Return the reversed string
    return joinArray; 
}
 
gReversedUserInput = reverseString(gUserInput); // gReversedUserInput and gUserInput MUST BE DEFINE IN THE WORKFLOW as GLOBAL variables

Hope this help!

2 Likes

Thank you very much, it has been of great help.

1 Like