去掉字符串str中,连续重复的地方
代码如下:
<!DOCTYPE html>
<html>
<body>
</body>
<script>
/*
去掉字符串str中,连续重复的地方
*/
function removeRepetition(str) {
var result = "";
var i;
for (i = 0; i < str.length; i++) {
if (str[i] != str[i + 1]) {
/*当下一个和自己不一样的时候 意味着自己现在是临界点 把自己添加进去 */
result = result + str[i];
}
// do something
}
return result;
}
// 测试用例
console.log(removeRepetition("aaa")); // ->a
console.log(removeRepetition("abbba")); // ->aba
console.log(removeRepetition("aabbaabb")); // ->abab
console.log(removeRepetition("")); // ->
console.log(removeRepetition("abc")); // ->abc
</script>
</html>