강의

멘토링

커뮤니티

Inflearn Community Q&A

hellooworld's profile image
hellooworld

asked

Introduction to Javascript Algorithm Problem Solving (Coding Test Preparation)

9. A to #

왜 answer+='#'를 사용하나요???

Written on

·

294

0

                let answer="";
                for(let x of s){
                    if(x=='A'answer+='#';
                    else answer+=x;
                }
                return answer;
           }

1. 왜 answer='#'이 아니라 answer+='#'을 해줘야 되는 건가요???

2. 그리고 +=을 사용하면 answer은 계속 샵 수가 증가하여 #,##,###이런식으로 저장돼서 B#N##N### 이렇게 결과값이 나와야되는거 아닌가요?? 왜 B#N#N#로 결과값이 나오는지 궁금합니다!!

javascript코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

1. answer='#' 과 같이 하면 answer 변수에 #하나만 저장되는 것만 반복합니다. answer에 A만 #으로 바꾼 문자열을 저장하기 위해 +연산자로 누적해주는 겁니다.

2. 무조건 #을 누적하는 것이 아니라 문자열에서 'A'를 만났을 때만 answer에 '#'을 누적하고, 'A'가 아니면 그냥 그 문자를 answer에 누적하고 있는 코드입니다.

hellooworld's profile image
hellooworld

asked

Ask a question