Your JavaScript function, swapNames, will be given a string including names, where the person's last name is followed by a comma and then the first name. You are to return a string with the same names but in first name then last name order, no commas. function swapNames(str) { Split the string by comma and create an array of names. should return the names in first name then last name order Use the split method of strings to split the input string and iterate over the resulting array. For each element, apply another split operation with the comma separator, then use array indexing to swap first and last names.
function swapNames(str) { var res = []; var arr = str.split(', '); for (var i = 0; i < arr.length; i++) { var obj = arr[i].split(' '); res.push(obj[1] + ' ' + obj[0]); } return res.join(', '); }