대문자로 이루어진 영어단어가 입력되면 단어에 포함된 ‘A'를 모두 ’#‘으로 바꾸어 출력하는 프로그램을 작성하세요.
첫 번째 줄에 문자열이 입력된다.
첫 번째 줄에 바뀐 단어를 출력한다.
BANANA
B#N#N#
function solution(string) {
let answer;
answer = string.split('A').join('#');
return answer;
}
console.log(solution('BANANA'));
function solution(string) {
let answer = '';
for (let i of string) {
if (i === 'A') {
answer += '#';
} else {
answer += i;
}
}
return answer;
}
console.log(solution('BANANA'));
1 . 정규식 이용, g는 글로벌
function solution(string) {
let answer = string;
answer = string.replace(/A/g, '#');
return answer;
}
console.log(solution('BANANA'));