【Codewars】<7kyu>Char Code Calculation

题目:

Given a string, turn each character into its ASCII character code and join them together to create a number - let's call this number total1:
'ABC' --> 'A' = 65, 'B' = 66, 'C' = 67 --> 656667

Then replace any incidence of the number 7 with the number 1, and call this number 'total2':

total1 = 656667
              ^
total2 = 656661
              ^
Then return the difference between the sum of the digits in total1 and total2:
  (6 + 5 + 6 + 6 + 6 + 7)
- (6 + 5 + 6 + 6 + 6 + 1)
-------------------------
                       6

题目翻译:给定一个字符串,将字符串的每一个字母转成ASCII码,然后拼接到一起,得到第一个数字`total1`,将`total1`中的数字`7`替换成数字`1`,得到第二个数字`total2`,最后返回total1中各位数字之和与total2中的各位数字之和的差

题解一:

// 题解一:
// match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。返回匹配结果的数组。
function calc(x){
    var xA = ''
    for(let i=0;i<x.length;i++){
        xA += x.substr(i,1).charCodeAt()
    }
    return (xA.match(/7/g) || []).length * 6;
}

题解二:

// 题解二:
// 通过replace()正则匹配每个字符去转成ASCII
const calc=x=>(x.replace(/./g,x=>x.charCodeAt()).match(/7/g)||[]).length*6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值