比较字符串的ascii分数-JavaScript
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
热门推荐
5 短祝福语简短暖心
10 结婚祝福语粤语大全简短
11 晚上祝福语女生文案简短
12 法语妈妈生日祝福语简短
13 药厂开工祝福语大全简短
14 蛋糕节日祝福语简短英文
15 跨年的生日祝福语简短
16 文案祝福语英文短句简短
17 在家聚餐婚礼祝福语简短
18 学生节祝福语大全简短