JavaScript Exercises Basic#9 - A to #

JavaScript Exercises Basic#9 - A to #

Write a program which replaces all 'A's in the letter with '#' when given an uppercase letter as an argument.

Result

Screenshot 2022-04-19 at 21.45.46.png

Solution1

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

Solution2

function solution(str){
                let answer = str; //making a shallow copy of the string
                answer = answer.replace(/A/g, '#'); //RegExp -> find 'A' globally and replace it with '#'

                return answer;   

           }