我很好奇是否存在一种以特定顺序从数组中构建字符串的方法.到目前为止,我的代码:
var pcontent = [ "h", "H", "o", " " ];
var constpass = strConstruct( "pcontent", 1, 2, 3, 0, 2, 3, 0, 2);
function strConstruct ( aname ) {
var newStrs = arguments;
var cs;
for ( var i = 1; i < newStrs.length; i++ ) {
cs = cs + aname[i];
}
return cs;
}
console.log( constpass );
运行后,我得到“ contentundefinedcontent”
如果不可能的话,那很高兴知道,谢谢
解决方法:
只是一些小错误
>您需要将变量pcontent传递给strConstruct而不是字符串“ pcontent”
>并且用aname [newStrs [i]]代替aname [i]
>将cs初始化为空字符串var cs =“”
var pcontent = ["h", "H", "o", " "];
var constpass = strConstruct(pcontent, 1, 2, 3, 0, 2, 3, 0, 2);
function strConstruct(aname) {
var newStrs = arguments;
var cs = "";
for (var i = 1; i < newStrs.length; i++) {
cs = cs + aname[newStrs[i]];
}
return cs;
}
console.log(constpass);