The Room Everyone Avoided

node v8.12.0
version: 1.0.0
endpointsharetweet
//Figure out which room no one claims to be the night of the murder from this data set const rooms = [ 'kitchen', 'ballroom', 'conservatory', 'dining room', 'billiard room', 'library' ]; const newDevelopment = [ { name: 'Miss Scarlet', present: true, rooms: [ {kitchen: false}, {ballroom: false}, {conservatory: true}, {'dining room': true}, {'billiard room': false}, {library: true} ] }, { name: 'Reverend Green', present: true, rooms: [ {kitchen: true}, {ballroom: false}, {conservatory: false}, {'dining room': false}, {'billiard room': true}, {library: false} ] }, { name: 'Colonel Mustard', present: true, rooms: [ {kitchen: false}, {ballroom: false}, {conservatory: true}, {'dining room': false}, {'billiard room': true}, {library: false} ] }, { name: 'Professor Plum', present: true, rooms: [ {kitchen: true}, {ballroom: false}, {conservatory: false}, {'dining room': true}, {'billiard room': false}, {library: false} ] } ]; //Reduce based solution. const unpopularRooms = rooms.filter(name => newDevelopment.every(s => !s.rooms.reduce((obj, room) => Object.assign(obj, room), {})[name] ) ); //All-filter based solution. const filteredRooms = rooms.filter(name => newDevelopment.every(s => !s.rooms.filter(room => name in room)[0][name] ) ); console.log('reduced', unpopularRooms); console.log('filtered', filteredRooms);
Loading…

no comments

    sign in to comment