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

Victor Warner
Victor Warner
1,518 Points

.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

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

<!DOCTYPE html>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
<script src="script.js"></script>

1 Answer

Steven Parker
Steven Parker
203,243 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.

Victor Warner
Victor Warner
1,518 Points

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
203,243 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.