대문자 찾기

jinny·2021년 8월 24일

Algorithm

목록 보기
7/34
post-thumbnail

해당 문자열에 알파벳 대문자가 몇 개 있는지 찾기

1. toUpperCase() 메서드 이용

function solution(str){

    let result = 0;
    for(let x of str) {
        if(x===x.toUpperCase()) result++;
    }

    return result;

  
}

let str = 'ComPuterProGramming' ;
console.log(solution(str));  // 4

toUpperCase() 메서드는 대문자로 바꿔주는 메서드

2. ASCII Code 이용

: charCodeAt() 메서드는 주어진 index에 해당하는 유니코드 값을 리턴

function solution(str){

    let result = 0;
    for(let x of str) {
      	let num = x.charCodeAt();
        if(num>=65 && num<=90) result++;
    }

    return result;

  
}

let str = 'ComPuterProGramming' ;
console.log(solution(str));  // 4

⇒ ASCII Code에서 대문자는 65~90, 소문자는 97~122

profile
주니어 개발자의 기록

0개의 댓글