Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

JavaScript에서 String.prototype.toUpperCase()를 사용하지 않고 대소문자 변경

<시간/>

문제

문자열 클래스의 프로토타입 개체에 있는 JavaScript 함수를 작성해야 합니다.

이 함수는 단순히 문자열에 있는 모든 알파벳의 대소문자를 대문자로 변경하고 새 문자열을 반환해야 합니다.

예시

다음은 코드입니다 -

const str = 'This is a lowercase String';
String.prototype.customToUpperCase = function(){
   const legend = 'abcdefghijklmnopqrstuvwxyz';
   const UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
   let res = '';
   for(let i = 0; i < this.length; i++){
      const el = this[i];
      const index = legend.indexOf(el);
      if(index !== -1){
         res += UPPER[index];
      }else{
         res += el;
      };
   };
   return res;
};
console.log(str.customToUpperCase());

출력

다음은 콘솔 출력입니다 -

THIS IS A LOWERCASE STRING