Returning part of a string data using map()

Hi good people! I’m trying to solve a JavaScript challenge, but my code is not working as expected

/*
PROBLEM: Write a function called getMainArtists which takes in an array of songs
and returns an array of the primary artists on the recordings.
If there's only one artist, that artist should be returned;
if there are featured artists, they should be ignored
(so only the artist to the left of "featuring" is kept.)
*/

//My Code Solution...

function getMainArtists(arr){
     return arr.map(function(val){
         if ((val.artist).includes("featuring")){
              return null;
             } else {
                    return val.artist;
              }
      })
}

getMainArtists(songs);

/*
The data I'm using can be found here https://github.com/PJMantoss/iterators2/blob/master/data.js
*/

PROBLEM STATEMENT: When I run getMainArtists(songs) it returns an array of artist names excluding names that contain ‘featuring’. It’s suppose to also return artist names that have ‘featuring’, but should only leave out all the words starting from ‘featuring’. Any ideas how I can refactor my code to work? Thank you for your help

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

instead you are doing this:

why are you returning null?

1 Like

Thank you Randell! I will try your solution

function getMainArtists(arr){
var newarr=;
arr.map(function(val){
if((val.artist).includes(“featuring”)){
newarr.push(val.artist.split(“featuring”)[0].trim());
}else{
newar.push(val.artist);
}
})
return newarr;
}

getMainArtists(songs);

1 Like

Thank you @ranotvijay

it returns an array of artist names excluding names

That keyword screams using filter instead of map, unless you really want to leave those null floating around.

You’re also probably forgetting this special case:

"Christina Aguilera, Lil' Kim, Mya, and Pink"

There are more than just that one but you catch m ydrift.