Advertisement
Guest User

Untitled

a guest
Mar 16th, 2022
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bullsAndCows(input) {
  2.  
  3.     let guessNumber = Number(input[0]);
  4.     let targetBulls = Number(input[1]);
  5.     let targetCows = Number(input[2]);
  6.  
  7.     let solutionFound = false;
  8.     let result = ""; //variable for result
  9.  
  10.     for (let digit1 = 1; digit1 <= 9; digit1++) {
  11.         for (let digit2 = 1; digit2 <= 9; digit2++) {
  12.             for (let digit3 = 1; digit3 <= 9; digit3++) {
  13.                 for (let digit4 = 1; digit4 <= 9; digit4++) {
  14.  
  15.                     let guessDigit1 = Math.floor(guessNumber / 1000) % 10;
  16.                     let guessDigit2 = Math.floor(guessNumber / 100) % 10;
  17.                     let guessDigit3 = Math.floor(guessNumber / 10) % 10;
  18.                     let guessDigit4 = Math.floor(guessNumber / 1) % 10;
  19.  
  20.                     let digitToCheck1 = digit1;
  21.                     let digitToCheck2 = digit2;
  22.                     let digitToCheck3 = digit3;
  23.                     let digitToCheck4 = digit4;
  24.  
  25.                     let currentBulls = 0;
  26.                     let currentCows = 0;
  27.  
  28.                     // Find all bulls, count them and remove them (assign -1 and -2)
  29.                     if (digitToCheck1 === guessDigit1) {
  30.                         // Bull at position #1 found -> count it and remove it
  31.                         currentBulls++;
  32.                         guessDigit1 = -1;
  33.                         digitToCheck1 = -2;
  34.                     }
  35.  
  36.                     if (digitToCheck2 === guessDigit2) {
  37.                         // Bull at position #2 found -> count it and remove it
  38.                         currentBulls++;
  39.                         guessDigit2 = -1;
  40.                         digitToCheck2 = -2;
  41.                     }
  42.  
  43.                     if (digitToCheck3 === guessDigit3) {
  44.                         // Bull at position #3 found -> count it and remove it
  45.                         currentBulls++;
  46.                         guessDigit3 = -1;
  47.                         digitToCheck3 = -2;
  48.                     }
  49.  
  50.                     if (digitToCheck4 === guessDigit4) {
  51.                         // Bull at position #4 found -> count it and remove it
  52.                         currentBulls++;
  53.                         guessDigit4 = -1;
  54.                         digitToCheck4 = -2;
  55.                     }
  56.  
  57.                     // Find all cows for digitToCheck1, count them and remove them (assign -1)
  58.                     if (digitToCheck1 === guessDigit2) {
  59.                         // Cow at position #2 found -> count it and remove it
  60.                         currentCows++;
  61.                         guessDigit2 = -1;
  62.  
  63.                     } else if (digitToCheck1 === guessDigit3) {
  64.                         // Cow at position #3 found -> count it and remove it
  65.                         currentCows++;
  66.                         guessDigit3 = -1;
  67.  
  68.                     } else if (digitToCheck1 === guessDigit4) {
  69.                         // Cow at position #4 found -> count it and remove it
  70.                         currentCows++;
  71.                         guessDigit4 = -1;
  72.                     }
  73.  
  74.                     // Find all cows for d2, count them and remove them (assign -1)
  75.                     if (digitToCheck2 === guessDigit1) {
  76.                         // Cow at position #1 found -> count it and remove it
  77.                         currentCows++;
  78.                         guessDigit1 = -1;
  79.  
  80.                     } else if (digitToCheck2 === guessDigit3) {
  81.                         // Cow at position #3 found -> count it and remove it
  82.                         currentCows++;
  83.                         guessDigit3 = -1;
  84.  
  85.                     } else if (digitToCheck2 === guessDigit4) {
  86.                         // Cow at position #4 found -> count it and remove it
  87.                         currentCows++;
  88.                         guessDigit4 = -1;
  89.                     }
  90.  
  91.                     // Find all cows for d3, count them and remove them (assign -1)
  92.                     if (digitToCheck3 === guessDigit1) {
  93.                         // Cows at position #1 found -> count it and remove it
  94.                         currentCows++;
  95.                         guessDigit1 = -1;
  96.  
  97.                     } else if (digitToCheck3 === guessDigit2) {
  98.                         // Cow at position #2 found -> count it and remove it
  99.                         currentCows++;
  100.                         guessDigit2 = -1;
  101.  
  102.                     } else if (digitToCheck3 === guessDigit4) {
  103.                         // Cow at position #4 found -> count it and remove it
  104.                         currentCows++;
  105.                         guessDigit4 = -1;
  106.                     }
  107.  
  108.                     // Find all cows for d4, count them and remove them (assign -1)
  109.                     if (digitToCheck4 === guessDigit1) {
  110.                         // Cows at position #1 found -> count it and remove it
  111.                         currentCows++;
  112.                         guessDigit1 = -1;
  113.  
  114.                     } else if (digitToCheck4 === guessDigit2) {
  115.                         // Cow at position #2 found -> count it and remove it
  116.                         currentCows++;
  117.                         guessDigit2 = -1;
  118.  
  119.                     } else if (digitToCheck4 === guessDigit3) {
  120.                         // Cow at position #3 found -> count it and remove it
  121.                         currentCows++;
  122.                         guessDigit3 = -1;
  123.                     }
  124.  
  125.                     if (currentBulls === targetBulls && currentCows === targetCows) {
  126.                         if (solutionFound) {
  127.                             //console.log(" ");
  128.                             result += " ";
  129.                         }
  130.  
  131.                         //console.log(`${digit1}${digit2}${digit3}${digit4}`);
  132.                         result += `${digit1}${digit2}${digit3}${digit4}`;
  133.                         solutionFound = true;
  134.                     }
  135.                 }
  136.             }
  137.         }
  138.     }
  139.  
  140.     if (!solutionFound) {
  141.         console.log('No');
  142.     }
  143.     else {
  144.         console.log(result);
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement