查找字符串的 ASCII 分数 - JavaScript

javascriptweb developmentfront end technologyobject oriented programming更新于 2025/12/12 14:36:17

ASCII 代码

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

相关文章


有用资源