Discuss / JavaScript / 第二题

第二题

Topic source

第二题: return s.split(" ").map(function(x) {return x 1;}).reduce(function (x, y) { return x 10 + y;});

function str2Int(s){ return s.split('').map(x=>+x).reduce((x,y)=>x*10+y); }

但是,其实,+s,就解决问题了。

庄纯良_

#3 Created at ... [Delete] [Delete and Lock User]

请教一下,为什么 return +x;可以返回一个字符数组。

s='123456';
s.split("").map(function(x){
   return +x;
});
[1, 2, 3, 4, 5]

冯毓权

#4 Created at ... [Delete] [Delete and Lock User]
/*使用一些符号也能做类型转换:*/

    X + '' // 等价于 String(X)
    +X    //  等价于 Number(X),也可以写成 x-0
    !!X   // 等价于 Boolean(X)

  • 1

Reply