2

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?

2
  • 2
    Why are you appending () to your array variable, its not a function Commented May 13, 2015 at 23:57
  • Why return newArray() instead of return newArray? Commented May 13, 2015 at 23:59

3 Answers 3

2

You 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

Sign up to request clarification or add additional context in comments.

Comments

2

you should return without parenthesis like so...

return newArray; 

3 Comments

hi, if you found my answer is correct please mark it as.
You should think about explaining why your answer is correct rather than just providing a "do this" answer. :)
yes you are right... this one was just a small typo :)
2

Quite likely it is unhappy with return newArray(); newArray is an array, not a function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.