0

I am trying to create a function which uses a parameter which will split up items. The split part works, but the problem is when I try to use an item from an array, I get undefined.

var arrayItems = ["twenty two","thirty three","forty four","fifty five"];

function splitElement(arr) {
    arr.split(" ");
    return arr;
}

splitElement(arrayItems[0]);

Unfortunately this returns twenty two instead of ["twenty","two"]

As a test, I tried this and it does return what I want:

function splitElement(arr) {
    arr = arrayItems[0].split(" ");
    return arr;
}

splitElement();

But what I want to do now is to grab any element from the array as a parameter and leave arr as the wildcard which can accept any input parameter for the output.

For example, if I did this:

splitElement("sixty six");

then it should return an array:

["sixty","six"];

What am I doing wrong?

4 Answers 4

4

You need to return the result of the split, since it doesn't change the original item.

Note: you're not splitting the array or an element (you pass an item from the array, but the function is not aware of that). I would rename the function to splitString(str).

var arrayItems = ["twenty two", "thirty three", "forty four", "fifty five"];

function splitElement(element) {
  return element.split(" ");
}

var result = splitElement(arrayItems[0]);

console.log(result);

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

Comments

2

String.split doesn't modify the original value, it returns a new array with the elements obtained by splitting the string. You need to store that array to use it or return it.

var arrayItems = ["twenty two","thirty three","forty four","fifty five"];
function splitElement(arr) {
    arr = arr.split(" ");
    return arr;
}

splitElement(arrayItems[0]);

Or, even simpler, just return the result directly instead of storing it in a variable:

var arrayItems = ["twenty two","thirty three","forty four","fifty five"];
function splitElement(arr) {
    return arr.split(" ");
}

splitElement(arrayItems[0]);

1 Comment

I already figured it out before you posted, but I will choose your answer (in a few minutes when it allows it) since it helped not only give the same solution as mine, but also explain why. Thank you!
0

Wow a lot of answers in moments. However, I solved it myself.

I made one mistake. What I needed to do was to define the variable in the function, then it will return the array.

var arrayItems = ["twenty two","thirty three","forty four","fifty five"];

function splitElement(arr) {
    arr = arr.split(" ");
    return arr;
}

splitElement(arrayItems[0]);

The only part I was missing was arr = before arr.split(" ");.

Comments

0

If you want it for all elements and get an array of arrays:

var arrayItems = ["twenty two", "thirty three", "forty four", "fifty five"];

function splitArrayElements(array){
  return array.map(item => item.split(" "));
}

var result = splitArrayElements(arrayItems);

console.log(result);

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.