I apologize if this has been seen before but I am here:
var fullName = ["Linus Trovalds "];
var birthYear = [1969];
var myArray = [fullName + birthYear];
console.log(myArray);
And I am trying to declare a variable named splitName, and set it equal to fullName split into two separate objects in an array using the split method. In other words, splitName should equal ["Linus", "Torvalds"] when printed to the console. How should this be written so that split name is printed to the console as mentioned above?
This split method is not working for me.
Any help will be greatly appreciated.
2 Answers 2
I don't know how you have tried it but on top of my head I think you might have forgotten that fullName variable is an array.
Try console.log(fullName[0].split(" "));
3 Comments
splitName[0] will give you the first index value. You should read more about arraysOr you could have simply split the fullName variable at the [5] mark to separate it into two strings in an array:
So it would be (for those looking for this in the future),
var fullName = ["Linus Trovalds"];
var birthYear = [1969];
var myArray = [fullName + birthYear];
var splitName = fullName.split(fullName[5])
//which would output ["Linus", "Trivolds"];
console.log(splitName)
//I changed this since I'm assuming you want the output to be the new array
.split(delimeter)is a method forStrings. In your code, the variablefullNameis an Array object because you define it with square brackets[ ]. The first and only element within yourfullNamearray is a string: sovar nameString = fullName[0]will give you back the name as a string. Then you can dovar firstLastArray = nameString.split(' ');to split on the space. Or you could just definevar fullName = "Linus Trovalds "; as a string instead of an array.birthYearfit into what you're asking about splitting the value of the name?