ASCII是7位字符代码,其中每个位代表一个唯一字符。
每个英文字母都有唯一的十进制ASCII码。
我们需要编写一个JavaScript函数,该函数接受一个字符串并计算该字符串字符的所有ASCII码之和
以下是代码-
const str = 'This string will be used for calculating ascii score';
const calculateASCII = str => {
let res = 0;
for(let i = 0; i < str.length; i++){
const num = str[i].charCodeAt(0);
res += num;
};
return res;
};
console.log(calculateASCII(str));输出结果
以下是控制台中的输出-
4946