Advertisement
nodejsdeveloperskh

bubble-sort

Oct 4th, 2021
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const blocks = [
  2.   {
  3.     gym: true,
  4.     school: true,
  5.     store: true,
  6.   },
  7.   {
  8.     gym: false,
  9.     school: true,
  10.     store: false,
  11.   },
  12.   {
  13.     gym: true,
  14.     school: false,
  15.     store: false,
  16.   },
  17.   {
  18.     gym: false,
  19.     school: true,
  20.     store: false,
  21.   },
  22.   {
  23.     gym: false,
  24.     school: true,
  25.     store: true,
  26.   },
  27. ];
  28. const reqs = ["gym", "school", "store"]
  29. const res = []
  30.  
  31. for (const blockIndex in blocks) {
  32.   let score = 0;
  33.   for (const req of reqs) {
  34.     if (blocks[blockIndex][req]) {
  35.       score++;
  36.     }
  37.   }
  38.   res.push({ block: blockIndex, score })
  39. }
  40.  
  41. console.log(res)
  42.  
  43. const sortedScores = bubbleSort(res.map(({score}) => score))
  44. const maximumBlockScore = sortedScores[sortedScores.length - 1];
  45. const desiredBlock = res.filter(({ score }) => score === maximumBlockScore);
  46.  
  47. console.log(desiredBlock);
  48.  
  49. function bubbleSort(array) {
  50.   for (let i = 0; i < array.length; i++) {
  51.     for (let j = 0; j < array.length; j++) {
  52.       if (array[j] > array[j + 1]) {
  53.         let temp = array[j];
  54.         array[j] = array[j + 1];
  55.         array[j + 1] = temp;
  56.       }
  57.     }
  58.   }
  59.   return array;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement