Returning part of a string data using map()
PJ Mantoss

PJ Mantoss @pjmantoss

About: I make web apps with HTML/CSS, Javascript, jQuery and ReactJS.

Location:
Abuja, Nigeria
Joined:
Sep 29, 2019

Returning part of a string data using map()

Publish Date: Feb 28 '20
3 6

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

Comments 6 total

  • diek
    diekFeb 28, 2020

    Here is one possible solution: repl.it/@DiegoMGar/getMainArtistss...

    • PJ Mantoss
      PJ MantossFeb 29, 2020

      Thank you Dieg! It worked like magic. Thanks a lot.

      • diek
        diekMar 1, 2020

        I think it is self explanatory but if you need to talk about the code just tell me :)

        • PJ Mantoss
          PJ MantossMar 1, 2020

          Yes Dieg! I need a little more explanation. I understand the code up to .split("featuring")[0], but what does .trim() do? Thank you

          • diek
            diekMar 1, 2020

            developer.mozilla.org/es/docs/Web/...
            I will take advantage of this question and recommend you the official documentation of MDN to search about Js implementations :)

            The trim erases the blanks at the start and the end of the string, in this case, as we splitted it by "featuring", your [0] can have an space at the end and maybe make fail your test, if you had, this is the reason of the removal :)

            • PJ Mantoss
              PJ MantossMar 2, 2020

              OK. I understand now. Thank you Dieg. Also, thanks a lot for the link

Add comment