문자열을 입력받아 해당 문자열을 처음부터 한 글자(letter)씩 다시 작성하려고 합니다. 이 때, 한 글자를 추가할 때마다 부분적으로 완성된 문자열을 전부 이어붙인 문자열을 리턴해야 합니다.
let output = makeMarginalString('abc');
console.log(output); // --> 'aababc'
output = makeMarginalString('flower');
console.log(output); // --> 'fflfloflowfloweflower'
output = makeMarginalString('');
console.log(output); // --> ''
a-> ab ->abc
- f->fl->flo->flow
이런식으로 흘러가는것을 파악할수있다
전체적으로 순회하면서 안쪽for문 reuslt값을 i값에따라
흘러가게만들면서 result값에 누적시키면 될거같다!
function makeMarginalString(str) {
// TODO: 여기에 코드를 작성합니다.
// /1 12 123
// a->ab->abc
// f->fl->flo->flow->flowe->flower
// 한번돌떄 전체순회햐면서
//result에 누적
// j =0 =>f // j=1 =>fl =>j=2=> fl
// result=f fl flo
let result="";
for(let i=0; i<str.length; i++){ // 초기값0
for(let j=0; j<=i; j++){ //초기값 1
result+=`${str[j]}`;
}
}
return result;
}