자바스크립트 글자 수 세기(feat. chatGPT)

chatGPT이 작성한 자바스크립트 글자 수 세기 코드입니다. 맛보기로 chatGPT에게 간단한 코드 작성을 요청해 보았는데요 직접 타이핑하는 것보다 빠르게 작성해 주네요.

자바스크립트-글자-수-세기

주석도 달아 주고 하단에는 설명까지 친절하게 알려줍니다.

앞으로 chatGPT에게 코드 작성을 요청하고 수정해서 사용하면 생산성이 높아질 것 같네요.

const str = '동해물과 백두산이 마르고 닳도록 하느님이 보우하사 우리나라 만세';
const letterCount = str.length; // 글자 수 (공백 포함)
const letterhWithoutSpaces = str.replace(/\s+/g, '').length; // 글자 수(공백 제외)
const spaceCount = (str.match(/ /g) || []).length; // 공백 수
const wordCount = str.trim().split(/\s+/).length; //단어 수

console.log(
  `공백포함 글자수: ${letterCount}, 공백제외 글자수: ${letterhWithoutSpaces}, 공백 수 ${spaceCount}, 단어 수: ${wordCount}`
);

// 결과 : 공백포함 글자수: 35, 공백제외 글자수: 28, 공백 수 7, 단어 수: 8
logo
아이티 이알