查找字符串的 ASCII 分数 - JavaScript


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));

Learn JavaScript in-depth with real-world projects through our JavaScript certification course. Enroll and become a certified expert to boost your career.

输出

以下是控制台中输出 −

4946

更新于: 18-Sep-2020

368 次浏览

启动你的 职业生涯

通过完成课程获得认证

开始
广告