Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript <;JS>;需要一个使用while循环添加“while”的函数&引用;到数组的字符串元素的末尾_Javascript_Arrays - Fatal编程技术网

Javascript <;JS>;需要一个使用while循环添加“while”的函数&引用;到数组的字符串元素的末尾

Javascript <;JS>;需要一个使用while循环添加“while”的函数&引用;到数组的字符串元素的末尾,javascript,arrays,Javascript,Arrays,尝试搜索此项失败。我需要使用while循环将“!!!”添加到字符串数组中每个元素的末尾。我尝试了几种不同的方法,最新的是: var facts = ["He was the last Beatle to learn to drive", "He was never a vegetarian", "He was a choir boy and boy scout", "He hated the sound of his own voice"]; function johnLennonFacts(

尝试搜索此项失败。我需要使用while循环将“!!!”添加到字符串数组中每个元素的末尾。我尝试了几种不同的方法,最新的是:

var facts = ["He was the last Beatle to learn to drive", "He was never a vegetarian", "He was a choir boy and boy scout",
"He hated the sound of his own voice"];

function johnLennonFacts(facts) {
var newFacts=[];
var i = 0;
while (i < 4) {

newFacts[i] = facts[i] +="!!!";
i++;
}
return newFacts;
}
var facts=[“他是最后一个学会驾驶的披头士乐队成员”,“他从来都不是素食主义者”,“他是唱诗班男孩和童子军”,
“他讨厌自己的声音”];
函数johnLennonFacts(facts){
var newFacts=[];
var i=0;
而(i<4){
newFacts[i]=事实[i]+=“!!!”;
i++;
}
返回新事实;
}
必须是我所知道的所有js无库。我是新的代码(显然)。我知道可能有更好的方法来实现这一点,但我必须使用while循环。提前感谢。

var facts=[“他是最后一个学会驾驶的披头士乐队成员”,“他从来都不是素食主义者”,“他是唱诗班男孩和童子军”,
var facts = ["He was the last Beatle to learn to drive", "He was never a vegetarian", "He was a choir boy and boy scout",
"He hated the sound of his own voice"];

function johnLennonFacts(facts) {
    var newFacts=[];
    var i = 0;
    while (i < facts.length) {
        newFacts[i] = facts[i] + "!!!";
        i++;
    }
    return newFacts;
}

console.log(johnLennonFacts(facts));
“他讨厌自己的声音”]; 函数johnLennonFacts(facts){ var newFacts=[]; var i=0; 而(i
试试这个:

var facts = ["He was the last Beatle to learn to drive", "He was never a vegetarian", "He was a choir boy and boy scout",
"He hated the sound of his own voice"];

function johnLennonFacts(facts) {
var newFacts=[];
var i = 0;
while (i < 4) {

newFacts[i] = facts[i] +"!!!";
i++;
}
return newFacts;
}
var facts=[“他是最后一个学会驾驶的披头士乐队成员”,“他从来都不是素食主义者”,“他是唱诗班男孩和童子军”,
“他讨厌自己的声音”];
函数johnLennonFacts(facts){
var newFacts=[];
var i=0;
而(i<4){
newFacts[i]=事实[i]+“!!!”;
i++;
}
返回新事实;
}

您不需要向字符串添加
=
,因为您已经在设置字符串。顺便说一句,
+=
是赋值运算符。另外,不要忘了在代码中调用函数。

如果您所要做的只是将“!!!”附加到数组项中-您可以在不创建新数组的情况下执行此操作-只需更改现有数组即可。以下控制台记录更改后的阵列。还要记住,仅仅拥有这个函数是不够的——为了让它工作——你还需要调用它。我还将其更改为将数组的长度作为上限-而不是硬编码ti为4-这更好,因为如果您更改数组的内容-您不必担心更改硬编码值

此外,“+=”运算符是表示以下内容的简写方式:…
事实[i]=事实[i]+“!!!”。。。;这在本例中有效,因为我们正在更改初始数组的值

//设置数组
var facts=[“他是最后一个学会驾驶的披头士乐队成员”,“他从来都不是素食者”,“他是一名唱诗班男孩和童子军”,
“他讨厌自己的声音”];
//调用时更改数组
函数johnLennonFacts(facts){
var i=0;
而(i约翰伦诺事实(事实)
这是有效的。我不必调用或console.log,因为我正在运行的JS在IDE中(对于一门课程来说),关于+=的澄清确实有帮助。感谢所有的帮助,我真的很感激

//设置数组
var facts=[“他是最后一个学会驾驶的披头士乐队成员”,“他从来都不是素食者”,“他是一名唱诗班男孩和童子军”,
“他讨厌自己的声音”];
//调用时更改数组
函数johnLennonFacts(facts){
var i=0;
而(ijohnLennonFacts(facts)
remove=from+=它将起作用,您需要更改初始数组或创建新数组吗?
+=
是赋值运算符。如果你只是连接的!!!只需使用
+