daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Oct 22nd, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function compress( string ) {
  2.  
  3.    
  4.     string = Array.from(string);
  5.    
  6.     let newString = [];
  7.  
  8.    
  9.     string.forEach( char => {
  10.        
  11.        
  12.         let count = 0;
  13.  
  14.  
  15.         newString.push( char );
  16.  
  17.  
  18.         for( i=0 ; i < string.length; i++){
  19.            
  20.             if ( string[i] === char ) { count += 1; }
  21.        
  22.         }
  23.  
  24.                
  25.         newString.push(count);
  26.  
  27.         if (  ( newString[newString.lastIndexOf(char)] === newString[newString.lastIndexOf(char) - 2]) ) {
  28.         newString.pop();
  29.         newString.pop();
  30.         }      
  31.  
  32.    
  33.     });
  34.  
  35.    
  36.     if ( newString.length >= string.length ) { return string; }
  37.    
  38.     else {
  39.        
  40.         newString = newString.toString();
  41.    
  42.         newString = newString.split(",").join(""); 
  43.         return newString;
  44.    
  45.     }
  46.  
  47.  
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top