0

I got this task and I can't get how to replace the matches with the string "BUSTED", can you help me please. I'm new to JS. Most probably there is a way more elegant way to do this. Any help appreciated.

You're given two arrays: one that holds every member of fCommunity and another one that holds every possible suspect. Replace every fCommunity member from the Suspect list with the word "BUSTED"

var fCommunityMembers = ['A','B','C'];
var SuspectList = ['F','X','B','Z','Y','C','ZS','D','K','M','N'];

I managed to retrieve the matching members but how do I replace them in the suspect list?:

Array.prototype.diff = function(SuspectList) {
    var ret = [];
    this.sort();
    SuspectList.sort();
    for(var i = 0; i < this.length; i += 1) {
        if(SuspectList.indexOf( this[i] ) > -1){
            ret.push( this[i] );
        }
    }
    return ret;
};

var ListOfMatches = Array.from(fCommunityMembers.diff(SuspectList));

console.log( ListOfMatches );
0

3 Answers 3

1

Use Array.prototype.map() to iterate fCommunityMembers. If an member is found in the SuspectList return BUSTED, if not return the member:

var fCommunityMembers = ['A','B','C'];
var SuspectList = ['F','X','B','Z','Y','C','ZS','D','K','M','N'];

var result = fCommunityMembers.map(function(member) {
  return SuspectList.indexOf(member) !== -1 ? 'BUSTED' : member;
});

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

What you need is a function that can do the intersection between two arrays

filter can help in your case

var fCommunityMembers = [
    'A',
    'B',
    'C',
    'D',
    'F'
];
var SuspectList = [
    'F',
    'X',
    'B',
    'Z',
    'Y',
    'C',
    'ZS',
    'D',
    'L'
];
var suspects= fCommunityMembers.filter(function(el){
    return SuspectList.indexOf(el)>-1;
}); 

Edit #1 To replace the values by busted, do the following:

var suspectsFnc = function (fc, sus) {
    var busted = fCommunityMembers.filter(function (el) {
        return SuspectList.indexOf(el) > -1;
    });

    for (var i = 0; i < fc.length; i++) {
        if (busted.indexOf(fc[i]) > -1) {
            fc[i] = "Busted";
        }
    }
    return fc;

}

var suspects = suspectsFnc(fCommunityMembers, SuspectList);

Comments

0

Judt use filter like this :

var ListOfMatches = SuspectList.filter(x => fCommunityMembers.indexOf(x) !== -1);

and a forEach loop to insert BUSTED instead of previous matched elements

fCommunityMembers.forEach((x,i,arr) => {
  if (SuspectList.indexOf(x) !== -1)
    arr[i] = "BUSTED";
});

2 Comments

Yes way more elegant to pick the matching members, thanks. How do I replace these common members with a string "BUSTED" in the CommunityMembers array?
@KathyBlue : answered above just now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.