JavaScript中字符串的ASCII总和差
ASCII码:
ASCII是7位字符代码,其中每个位代表一个唯一字符。每个英文字母都有唯一的十进制ASCII码。
我们需要编写一个函数,该函数接受两个字符串并计算它们的ascii分数(即,每个字符串字符的ascii十进制总和)并返回差值。
让我们为该函数编写代码-
示例
为此的代码将是-
const str1 = 'This is an example sting';
const str2 = 'This is the second string';
const calculateScore = (str = '') => {
return str.split("").reduce((acc, val) => {
return acc + val.charCodeAt(0);
}, 0);
};
const ASCIIDifference = (str1, str2) => {
const firstScore = calculateScore(str1);
const secondScore = calculateScore(str2);
return Math.abs(firstScore - secondScore);
};
console.log(ASCIIDifference(str1, str2));输出结果
控制台中的输出-
116
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短