Advertisement
Lulunga

Text Processing 05. Replace Repeating Chars

Jul 19th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(string) {
  2.     let updatedStr = string[0];
  3.     let currentChar = string[0];
  4.     for (let i = 1; i < string.length; i++) {
  5.         if (string[i] !== currentChar) {
  6.             updatedStr += string[i];
  7.             currentChar = string[i];
  8.         }
  9.  
  10.     }
  11.     console.log(updatedStr);
  12. }
  13.  
  14. /*function solve(string) {
  15.     let output = string[0];
  16.     for (let i = 1; i < string.length; i++) {
  17.         let current = string[i];
  18.         let previous = string[i - 1];
  19.         if (current !== previous) {
  20.             output += current;
  21.         }
  22.  
  23.     }
  24.     console.log(output);
  25. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement