Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects Create an Empty JavaScript Object

.split for two strings

hi, when I use the return statement to pass two strings into my first window.alert method, it seems to work alert//vicvi. However afterwards it says that split for name2 is undefined and breaks the rest of the code. I don't quite understand what is happening. Also how would you call two parameters from a function do you have to do a conditional statement?

Thank You

script.js
var myArray = [];
myArray = ["vic", "vic"];
var cutName = function(name, _name2) {

    return name.split(", ") + _name2.split(', '); 

}
window.alert(cutName("vic", "vi"));
window.alert(cutName( "yellow"));

var myInfo= {
  fullName: cutName(myArray[0]),
     favoriteColor: myArray[1],
    github: myArray[2] || null

}
console.log(myInfo);
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

1 Answer

Steven Parker
Steven Parker
231,236 Points

The function "cutName" is defined to take two arguments (name and _name2). So later in the code:

window.alert(cutName("vic", "vi"));  // call has two arguments, no problem
window.alert(cutName( "yellow"));    // call has only ONE argument!

Since the second call does not have a second argument, "_name2" remains undefined and breaks the function.

The same thing happens here:

  fullName: cutName(myArray[0]),    // call has only ONE argument!

If you wanted to pass the array elements as separate arguments you could do this:

  fullName: cutName(...myArray),

One last comment, it's not clear why you are using "split" here at all since none of the values contain the sequence it needs to work.

Steve is the last bit of code es6 also originailly the problem was asking for two strings to be created and then spererated into their own arrays. Thank You for answering.

Steven Parker
Steven Parker
231,236 Points

Can you provide a link to the course page you saw that on? The link button in the upper right of this question appears to be for something else.