Javascript 如何调用使用Split函数创建的数组?
我正在做一个编程挑战,正在努力解决一个问题(重点是粗体) 以下是挑战:Javascript 如何调用使用Split函数创建的数组?,javascript,arrays,function,split,Javascript,Arrays,Function,Split,我正在做一个编程挑战,正在努力解决一个问题(重点是粗体) 以下是挑战: 首先,声明一个名为myArray的数组 太好了!现在用两个字符串填充myArray。在第一个字符串中输入您的全名,在第二个字符串中输入您的Skype句柄 接下来,声明一个名为cutName的函数。它应该以字符串作为参数 cutName应通过将输入字符串拆分为单个单词来返回数组。例如,“Douglas Crockford”应作为[“Douglas”,“Crockford”]返回 创建一个名为myData的新空对象文本。 按照以
myArray
的数组myArray
。在第一个字符串中输入您的全名,在第二个字符串中输入您的Skype句柄cutName
的函数。它应该以字符串作为参数cutName
应通过将输入字符串拆分为单个单词来返回数组。例如,“Douglas Crockford”应作为[“Douglas”,“Crockford”]
返回myData
的新空对象文本。
myData
:myArray
中的名称字符串调用cutName
6.2. skype:在
myArray
中引用您的skype句柄6.3. github:如果您有github句柄,请将其作为字符串输入。如果不是,则将其设置为空。
我的回答是:
var myArray = ["Arthur Philadelpho", "arthurphiladelpho"];
function cutName (myArray){return myArray.split(" "); }
var myData = {**fullname:myArray[0]**, skype:myArray[1], github:null};
我对第6.1项有疑问。调用“cutName(myData.fullname)”
var myArray=[“Arthur Philadelpho”,“Arthur Philadelpho”];
函数cutName(myArray){返回myArray.split(“”;}
var myData={fullname:myArray[0],skype:myArray[1],github:null};
log(cutName(myData.fullname))代码>
调用“cutName(myData.fullname)”
var myArray=[“Arthur Philadelpho”,“Arthur Philadelpho”];
函数cutName(myArray){返回myArray.split(“”;}
var myData={fullname:myArray[0],skype:myArray[1],github:null};
log(cutName(myData.fullname))代码>
您应该对名称字符串而不是整个数组执行拆分
像这样:
myArray[0].split(" ")
这将返回一个包含两个单词的新数组
以下是完整的任务:
var myArray = [];
myArray.push('Pepito Perez');
myArray.push('pepitoperez');
function cutName (str) {
return str.split(' ');
}
var myData = {
fullname: cutName(myArray[0]),
skype: myArray[1],
github: 'http://ww.github.com/...'
}
好了,这并不难
代码解释
cutName(myArray[0]) //=> returns ["Arthur", "Philadelpho"] as requested
cutName(myArray[0])[0] //=> access the first element on that array: "Arthur"
您的代码中有两个错误:
/**
* This part is fine, you are defining your initial array with two string in it.
*/
var myArray = ["Arthur Philadelpho", "arthurphiladelpho"];
/**
* At this part you must create a function that takes an string to split it in
* two parts and return an array with those part in it.
* This function will return the splitted array once you call it, but in your
* code you never do that.
* Also, you are taking an argument that is your original array and must be the
* first Item of your array (your fullname), so you should call your function
* like this cutName(myArray[0]);, That way, you are passing the string to the
* function, instead of the whole array.
* Inside the function you should split your string the way you are doing, but I
* recomend you to change the argument name for something like 'str' for
* example, that way you wont get confused and won't be any change of shadowing
* your code.
*/
function cutName (myArray){return myArray.split(" "); }
/**
* Finally you have to call your function inside your object literal delaration
* for the fullname key passing the first element of myArray as the function
* parameter like this "fullname: cutName(myArray[0])"
*/
var myData = {**fullname:myArray[0]**, skype:myArray[1], github:null};
您应该对名称字符串而不是整个数组执行拆分
像这样:
myArray[0].split(" ")
这将返回一个包含两个单词的新数组
以下是完整的任务:
var myArray = [];
myArray.push('Pepito Perez');
myArray.push('pepitoperez');
function cutName (str) {
return str.split(' ');
}
var myData = {
fullname: cutName(myArray[0]),
skype: myArray[1],
github: 'http://ww.github.com/...'
}
好了,这并不难
代码解释
cutName(myArray[0]) //=> returns ["Arthur", "Philadelpho"] as requested
cutName(myArray[0])[0] //=> access the first element on that array: "Arthur"
您的代码中有两个错误:
/**
* This part is fine, you are defining your initial array with two string in it.
*/
var myArray = ["Arthur Philadelpho", "arthurphiladelpho"];
/**
* At this part you must create a function that takes an string to split it in
* two parts and return an array with those part in it.
* This function will return the splitted array once you call it, but in your
* code you never do that.
* Also, you are taking an argument that is your original array and must be the
* first Item of your array (your fullname), so you should call your function
* like this cutName(myArray[0]);, That way, you are passing the string to the
* function, instead of the whole array.
* Inside the function you should split your string the way you are doing, but I
* recomend you to change the argument name for something like 'str' for
* example, that way you wont get confused and won't be any change of shadowing
* your code.
*/
function cutName (myArray){return myArray.split(" "); }
/**
* Finally you have to call your function inside your object literal delaration
* for the fullname key passing the first element of myArray as the function
* parameter like this "fullname: cutName(myArray[0])"
*/
var myData = {**fullname:myArray[0]**, skype:myArray[1], github:null};
对此的快速答案是:
var myArray = ["Arthur Philadelpho", "arthurphiladelpho"];
function cutName (nameAndSurname){ return nameAndSurname.split(" "); }
var myData = { fullName: cutName(myArray[0])[0], skype:myArray[1], github:null};
解释
cutName(myArray[0]) //=> returns ["Arthur", "Philadelpho"] as requested
cutName(myArray[0])[0] //=> access the first element on that array: "Arthur"
我建议您这样做以使代码更加清晰:
var myData = {};
myData.fullName = cutName(myArray[0])[0];
myData.skype = myArray[1];
myData.github = null;
对此的快速答案是:
var myArray = ["Arthur Philadelpho", "arthurphiladelpho"];
function cutName (nameAndSurname){ return nameAndSurname.split(" "); }
var myData = { fullName: cutName(myArray[0])[0], skype:myArray[1], github:null};
解释
cutName(myArray[0]) //=> returns ["Arthur", "Philadelpho"] as requested
cutName(myArray[0])[0] //=> access the first element on that array: "Arthur"
我建议您这样做以使代码更加清晰:
var myData = {};
myData.fullName = cutName(myArray[0])[0];
myData.skype = myArray[1];
myData.github = null;
以下是黑客反应堆挑战代码:
var myArray=[];
myArray[0]=“Rakib Hossain”;
myArray[1]=“rakib.csit”;
函数名(){
}
函数cutName(myArray){返回myArray.split(“”;}
var myData={};
var myData={
全名:cutName(myArray[0]),
skype:myArray[1],
github:null
}
Admissions.showApp()代码>以下是黑客反应堆挑战代码:
var myArray=[];
myArray[0]=“Rakib Hossain”;
myArray[1]=“rakib.csit”;
函数名(){
}
函数cutName(myArray){返回myArray.split(“”;}
var myData={};
var myData={
全名:cutName(myArray[0]),
skype:myArray[1],
github:null
}
Admissions.showApp()代码>这应该适用于您:
var myArray = []
myArray = (["Lynette Sandoval", "red"]);
function cutName(myArray) { return myArray.split(" "); }
var myInfo = {};
name = cutName(myArray[0]); // calling cutname with myArray
Object.assign(myInfo, {
fullName: name,
favoriteColor: myArray[1],
github: null
});
这应该适合您:
var myArray = []
myArray = (["Lynette Sandoval", "red"]);
function cutName(myArray) { return myArray.split(" "); }
var myInfo = {};
name = cutName(myArray[0]); // calling cutname with myArray
Object.assign(myInfo, {
fullName: name,
favoriteColor: myArray[1],
github: null
});
您不调用数组,而是调用函数(在本例中为cutName
。您已经知道如何访问数组的成员(当您访问skype名称时);现在只需以相同的方式访问全名,并将其作为参数调用cutName
。此外,split返回一个数组,您只需要访问一个成员,而不是整个数组。您不调用数组,而是调用函数(在本例中为cutName
。您已经知道如何访问数组的成员(当您访问skype名称时);现在只需以相同的方式访问全名,并以该参数调用cutName
。此外,split返回一个数组,您只需要访问一个成员,而不是整个数组。这不起作用…我仍然没有完全理解问题,但此建议没有起作用。有什么想法吗?这不起作用…我仍然没有完全理解y理解这个问题,但这个建议没有奏效。有什么想法吗?谢谢Dave我已经解决了这个问题。我的新问题是问题6.1这行代码fullname:cutName(myArray[0])
是第6.1点的答案。看看函数。谢谢Dave,我已经解决了这个问题。我的新问题是第6.1问题。这行代码全名:cutName(myArray[0])
是第6.1点的答案。看看函数。第6.1点说“在myArray中存储的名称字符串上调用cutName”,而不是将第一项存储在全名数组中。@Dave Gomez感谢您的反馈,我在接受此挑战时学到了不少东西,但仍然无法完成此挑战。有些事情仍然被认为是不正确的:我用新代码更新了问题。@ArthurPhiladelpho我将解释您更新我答案的当前代码。point 6.1表示“对存储在myArray中的名称字符串调用cutName”,而不是将第一项存储在全名数组中。@Dave Gomez感谢您的反馈,我在接受此挑战时学到了不少东西,但仍然无法完成此挑战。仍有一些问题被认为是不正确的:我用新代码更新了问题。@ArthurPhiladelpho我将解释更新我答案的当前代码。