admin管理员组文章数量:1320661
Very new to Javascript and not understanding why my tutorial isn't accepting my code as an answer...
Challenge is to create a function that returns an array after breaking up string into separate words.
Here's what I have so far:
function cutName(namestr) {
var newArray = namestr.split(' ');
return newArray();
}
This seems to work when called, for example returning the following when given this string "hello does this work" as an argument:
[ 'hello', 'does', 'this', 'work' ]
What the heck am I doing wrong here? Shouldn't the above code suffice for an answer?
Very new to Javascript and not understanding why my tutorial isn't accepting my code as an answer...
Challenge is to create a function that returns an array after breaking up string into separate words.
Here's what I have so far:
function cutName(namestr) {
var newArray = namestr.split(' ');
return newArray();
}
This seems to work when called, for example returning the following when given this string "hello does this work" as an argument:
[ 'hello', 'does', 'this', 'work' ]
What the heck am I doing wrong here? Shouldn't the above code suffice for an answer?
Share Improve this question asked May 13, 2015 at 23:56 AdjunctProfessorFalconAdjunctProfessorFalcon 1,8407 gold badges29 silver badges70 bronze badges 2-
2
Why are you appending
()
to your array variable, its not a function – Patrick Evans Commented May 13, 2015 at 23:57 - Why return newArray() instead of return newArray? – cubanGuy Commented May 13, 2015 at 23:59
3 Answers
Reset to default 2You need to remove the parenthesis from return newArray;
. When learning JavaScript, you might want to look into tools like JSBin, they give you a lot of helpful feedback and realtime results.
JavaScript
function cutName(namestr) {
var newArray = namestr.split(' ');
return newArray;
}
var arr = cutName('hello does this work');
console.log(Array.isArray(arr));
console.log(arr);
console output
true
["hello", "does", "this", "work"]
See the JSBin
you should return without parenthesis like so...
return newArray;
Quite likely it is unhappy with return newArray();
newArray is an array, not a function.
本文标签: Returning array in Javascript function after splitting stringStack Overflow
版权声明:本文标题:Returning array in Javascript function after splitting string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742064499a2418751.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论