1
var myList = ["Hello There World", "meatball", "cookie monster"];

var BreakUpFirst = function (myList) {
document.write(myList[0].split(" "));
};

Hello I am new to JavaScript so please bear with me. Not sure what I am doing wrong here.

I am trying to break the first string in the myList array into its own array with 3 values. The split method works fine for me outside of the function, but I can't get it to work inside. I want to create BreakUpFirst as a new array and keep myList or any other array I pass through it as is.

4
  • 2
    Why not just var BreakUpFirst = myList[0].split(" ");? Commented Jan 19, 2014 at 6:40
  • you're not executing the function - you're just defining one. you'd need BreakUpFirst() afterwards somewhere to actually RUN the code... Commented Jan 19, 2014 at 6:41
  • Still getting an error when I run the code after. I am trying to learn how to pass arrays through functions and would like the array to be the argument of the function. JavaScript error: Uncaught TypeError: Cannot read property '0' of undefined on line 5 Commented Jan 19, 2014 at 6:48
  • That's because you have to pass in your array the way you've defined your function. If you want to use the global myList array you've already defined, take myList out of your function definition so it just reads var BreakUpFirst = function() { Alternative, you can invoke your function as indicated in my answer below, passing in the global variable. Commented Jan 19, 2014 at 7:00

1 Answer 1

1

You're actually doing a couple of different things here. If you just want to assign the value of myList[0].split(" ") to var BreakUpFirst, then the solution suggested by @thefourtheye is ideal. Just assign the value directly:

var BreakUpFirst = myList[0].split(" ");

If you're trying to use a function that will always break up a string stored at the first element of an array and output it to the screen, then you need to make sure you pass in the array as a parameter. If you define your function:

 var BreakUpFirst = function(myList) {
     return myList[0].split(" ");
 }

 var myList = ["Hello There World", "meatball", "cookie monster"];

You need to make sure you invoke the function and pass in the parameter:

 var brokenString = BreakUpFirst(myList);
 alert(brokenString);

You will get an alert box with the brokenString array, "Hello","There","World"

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

2 Comments

This makes a lot of sense, but I am still getting undefined in the alert box. Was hoping to get "There" in a pop up for alert(brokenString[1]); Thanks for your help!
@user3211495: The problem is the use of document.write(). That should just be a return. I've edited the answer since it seems pretty clear that's what citizenslave meant to be there.

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.