Issue
I am creating an app that gives rewards to the users, so they can obtain randomly complements to their avatars. I have a list of items that they can win and another list of items that they already have. My problem is that I don't know how to look for a match between the two arrays and create another without the ones that they already have.
var availableAvatar =['Csimple','Calien','Ccosmonaut','CgreenAereal','ChappyBirthday']
var userAvatars=['Ccosmonaut','ChappyBirthday']
I tried with the filter method but it creates an array of the matches and I don't know how to do it the other way. What I need:
var possibleAward=['Csimple','Calien','CgreenAereal']
var random = avatarP[Math.floor(Math.random() * possibleAward.length)];
Thank you very much.
Solution
The array filter
function is perfect for this:
var availableAvatars = ['Csimple','Calien','Ccosmonaut','CgreenAereal','ChappyBirthday']
var userAvatars = ['Ccosmonaut','ChappyBirthday']
var possibleAvatars = availableAvatars.filter(x => !userAvatars.includes(x));
var randomAvatar = possibleAvatars[Math.floor(Math.random() * possibleAvatars.length)];
console.log(possibleAvatars);
console.log(randomAvatar);
Answered By - Robson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.