function compressString(str) {
for(let i =0 ; i < str.length; i++) {
let count = 1;
let newArr = '';
for(let n = i+1; n < str.length; n++) {
if (str[i]===str[n]){
newArr = newArr + str[n];
count++
} else {
break;
}
}
if (count > 2) {
str = str.replace(newArr, count);
}
}
return str;
}
-----------------------------------
function compressString(str) {
let count = 1;
let newArr = '';
for(let i =0 ; i < str.length; i++) {
for(let n = i+1; n < str.length; n++) {
if (str[i]===str[n]){
newArr = newArr + str[n];
count++
} else {
break;
}
}
if (count > 2) {
str = str.replace(newArr, count);
}
}
return str;
}
let count = 1;
let newArr = ''; 이 변수들이 전역변수가 되는 것과 for문 안에 있을 때의 차이는 ?
댓글