Isaiah Sias
Isaiah Sias

Reputation: 31

Break an input string into individual words

Problem:

My code

var myArray = ['Isaiah Sias', 'isaiahsias15689'];
function cutName(name){
    var splitString = name.split(myArray[0]' ');
    return splitString;
}

var myInfo{
  fullName = cutName(myArray[0]),
  skype = myArray[1],
  github='@kakashihatake',
};

Once again I am not sure where I am messing up. I have been working on this problem for a few days now and find myself a little frustrated.

Upvotes: 2

Views: 1931

Answers (2)

alessandrio
alessandrio

Reputation: 4370

var myArray = ['Isaiah Sias', 'isaiahsias15689'],
    github = '@kakashihatake';
function toObject(){
    return {
      fullName: myArray[0].split(' '),
      skype: myArray[1],
      github: github
    }
}

console.log(toObject());

Upvotes: -1

James
James

Reputation: 22247

You are very close, you have made a small mistake in the cutName function.

The string.split method takes only 1 parameter, the string to split by. You've tried to pass in the array element as well. Get rid of it! (Keep in mind that the thing we are splitting, name, has been assigned the array element as its value during the function call)

var splitString = name.split(myArray[0]' ');

becomes

var splitString = name.split(' ');

One other issue, you'll need to change your object definition a bit. You have a missing = between myInfo and the start of the object literal. And, when setting property names and values in an object literal you need to use colon instead of equals, so your object

var myInfo{
fullName = cutName(myArray[0]),
skype = myArray[1],
github='@kakashihatake',
};

becomes

var myInfo = {
  fullName: cutName(myArray[0]),
  skype: myArray[1],
  github: '@kakashihatake'
};

Upvotes: 2

Related Questions