Discuss / JavaScript / 难度大增了

难度大增了

Topic source

廖i雪

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

// jslearning.js

// 求积 return arr.reduce(function (x,y){return x * y;});

// string2int 函数 return arr.split('').map(function (x){ return +x;}).reduce(function (x,y){return x * 10 + y;});

//英文名字格式化 return arr.map(function (name) {return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase();});

//小明的代码怎么了? r = arr.map(function(x){return parseInt(x)}); //感谢,抄来的,费了好大劲,似懂非懂

铁头198009

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

map 里面的 return +x; 直接就返回数字了... 这个思路不对吧

astak3

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

split得到的valuestring,用map 里面的 return +x是把value,变成number,如果不做这一步,最后reduce的结果是string不是number

因为parseInt其实还有第二个参数:radix,该参数为进行转换的基数,需要是2~36的一个值,默认为0是使用10,map函数实际会为它传入第二个参数:当前数的索引值。所以parseInt实际拿到的radix值在传入1时为0,所以无误,传入2时为1,返回NaN,传入3时为2,返回NaN

何森康丶

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

<pre>//小明的代码怎么了? r = arr.map(Number); //这样也可以 不知道对不对希望大神指点。 </pre>

何森康丶

#6 Created at ... [Delete] [Delete and Lock User]
//小明的代码怎么了?
r = arr.map(Number); //这样也可以   不知道对不对希望大神指点。

r=arr.map(String); 怎么这样写它输入的也是1,2,3 呢?


  • 1

Reply