首页 > 解决方案 > 以下代码片段中的最小更改是什么,以使输出为“ABC”?

问题描述

您好,我的功能有这个问题

const string = ['a', 'b', 'c'].reduce((acc, x) => x.concat(x.toUpperCase()));
console.log(string );

在最终结果中我想得到“ABC”

标签: javascript

解决方案


你需要做两件事

  • 适用concat()acc不与x
  • 通过将其作为第二个参数传递来设置accto的初始值''reduce()
  • 您可以使用+而不是contat()

const string = ['a', 'b', 'c'].reduce((acc, x) => acc+x.toUpperCase(),'');
console.log(string );

您也可以使用map()join()

const string = ['a', 'b', 'c'].map(x=>x.toUpperCase()).join('')
console.log(string );


推荐阅读