Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const blocks = [
- {
- gym: true,
- school: true,
- store: true,
- },
- {
- gym: false,
- school: true,
- store: false,
- },
- {
- gym: true,
- school: false,
- store: false,
- },
- {
- gym: false,
- school: true,
- store: false,
- },
- {
- gym: false,
- school: true,
- store: true,
- },
- ];
- const reqs = ["gym", "school", "store"]
- const res = []
- for (const blockIndex in blocks) {
- let score = 0;
- for (const req of reqs) {
- if (blocks[blockIndex][req]) {
- score++;
- }
- }
- res.push({ block: blockIndex, score })
- }
- console.log(res)
- const sortedScores = bubbleSort(res.map(({score}) => score))
- const maximumBlockScore = sortedScores[sortedScores.length - 1];
- const desiredBlock = res.filter(({ score }) => score === maximumBlockScore);
- console.log(desiredBlock);
- function bubbleSort(array) {
- for (let i = 0; i < array.length; i++) {
- for (let j = 0; j < array.length; j++) {
- if (array[j] > array[j + 1]) {
- let temp = array[j];
- array[j] = array[j + 1];
- array[j + 1] = temp;
- }
- }
- }
- return array;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement