문자의 무게(알파벳):
영어 알파벳의 가중치는 1부터 시작하는 인덱스가 아닙니다.
예를 들어 'c'의 가중치는 3, 'k'는 11 등입니다.
소문자 문자열을 받아 해당 문자열의 가중치를 계산하고 반환하는 JavaScript 함수를 작성해야 합니다.
예시
이에 대한 코드는 -
const str = 'this is a string';
const calculateWeight = (str = '') => {
str = str.toLowerCase();
const legend = 'abcdefghijklmnopqrstuvwxyz';
let weight = 0;
const { length: l } = str;
for(let i = 0; i < l; i++){
const el = str[i];
const curr = legend.indexOf(el);
weight += (curr + 1);
};
return weight;
};
console.log(calculateWeight(str)); 출력
콘솔의 출력은 -
172