JSFiddle - React, Tailwind, and code Playground

by konijn_gmail_com

JavaScript

function isLetter( c )
{
  c = c?c+"":"_";
  return ( c.toLowerCase() != c.toUpperCase() )
}

function SwapII(str) { 
  
  var a = str.split(""),
      shouldSwap =false,
      swapIndex,
      c,t;
      
  
  for( i = 0 ; i < a.length ; i++ )
  {
    c = a[i];
    if(  !isNaN(c*1) && c != " " )
    {
      if( shouldSwap )
      {
        t = c;
        a[i] = a[swapIndex];
        a[swapIndex] = t;
        shouldSwap = false
      }
      else if(  isLetter( a[i+1] ) )
      {
        shouldSwap = true;  
        swapIndex = i;
      } 
       
    }
    else if( c != c.toLowerCase() )
    {
      a[i] = c.toLowerCase();  
    }
    else if( c != c.toUpperCase() )
    {
      a[i] = c.toUpperCase();
    }
    else
    {
      shouldSwap = false;
    }
    
  }
  return a.join("")
         
}
   
// keep this function call here 
// to see how to enter arguments in JavaScript scroll down
console.log( SwapII( "2S 6 du5d4e" ) );