Returning part of a string data using map()

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){
         if ((val.artist).includes("featuring")){
              return null;
             } else {
                    return val.artist;


The data I'm using can be found here

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.

instead you are doing this:

why are you returning null?

1 Like

What if instead of checking whether or not the word featuring is in val.artist, you just create a new variable (i.e. mainArtist) and assign the result of stripping out only the artist name that appears before “featuring” and return mainArtist? Using the correct regex with the replace method, this becomes a simple one line function.

1 Like

Thank you Randell! I will try your solution