ASCII 코드
ASCII는 모든 단일 비트가 고유한 문자를 나타내는 7비트 문자 코드입니다.
모든 영어 알파벳에는 고유한 십진수 ASCII 코드가 있습니다.
두 개의 문자열을 받아서 ASCII 점수(즉, 문자열의 각 문자의 ASCII 십진수 합계)를 계산하고 그 차이를 반환하는 함수를 작성해야 합니다.
예시
이에 대한 코드를 작성해 보겠습니다 -
const str1 = 'This is the first string.';
const str2 = 'This here is the second string.';
const calculateScore = (str = '') => {
return str.split("").reduce((acc, val) => {
return acc + val.charCodeAt(0);
}, 0);
};
const compareASCII = (str1, str2) => {
const firstScore = calculateScore(str1);
const secondScore = calculateScore(str2);
return Math.abs(firstScore - secondScore);
};
console.log(compareASCII(str1, str2)); 출력
다음은 콘솔의 출력입니다 -
536