Skip to content

Instantly share code, notes, and snippets.

@anuragkapur
Created December 24, 2019 01:24
Show Gist options
  • Save anuragkapur/0c7f70dacfb17027036b44fbcd263e4e to your computer and use it in GitHub Desktop.
Save anuragkapur/0c7f70dacfb17027036b44fbcd263e4e to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/kolutan
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// For the data set given below, find room which is false for all suspects (objects in the newDevelopment array)
'use strict';
var 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 }]
}];
function intersection(rooms1, rooms2) {
var intersection = [];
rooms1.forEach(function (room) {
if (rooms2.includes(room)) {
intersection.push(room);
}
});
return intersection;
}
var listOfListOfRooms = [];
newDevelopment.forEach(function (suspect) {
var listOfRooms = suspect.rooms.filter(function (room) {
return Object.values(room).includes(false);
}).flatMap(function (room) {
return Object.keys(room);
});
listOfListOfRooms.push(listOfRooms);
});
console.log(listOfListOfRooms.reduce(intersection));
</script>
<script id="jsbin-source-javascript" type="text/javascript">// For the data set given below, find room which is false for all suspects (objects in the newDevelopment array)
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}
]
}
];
function intersection(rooms1, rooms2) {
const intersection = [];
rooms1.forEach(room => {
if (rooms2.includes(room)) {
intersection.push(room);
}
});
return intersection;
}
const listOfListOfRooms = [];
newDevelopment.forEach(suspect => {
const listOfRooms = suspect.rooms
.filter(room => Object.values(room).includes(false))
.flatMap(room => Object.keys(room));
listOfListOfRooms.push(listOfRooms);
});
console.log(listOfListOfRooms.reduce(intersection));</script></body>
</html>
// For the data set given below, find room which is false for all suspects (objects in the newDevelopment array)
'use strict';
var 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 }]
}];
function intersection(rooms1, rooms2) {
var intersection = [];
rooms1.forEach(function (room) {
if (rooms2.includes(room)) {
intersection.push(room);
}
});
return intersection;
}
var listOfListOfRooms = [];
newDevelopment.forEach(function (suspect) {
var listOfRooms = suspect.rooms.filter(function (room) {
return Object.values(room).includes(false);
}).flatMap(function (room) {
return Object.keys(room);
});
listOfListOfRooms.push(listOfRooms);
});
console.log(listOfListOfRooms.reduce(intersection));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment