davegimo

firstpos2

Dec 5th, 2019
160
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var arr1 = [50,20,2,1,3,5];
  2.  
  3. var arr = [50,20,2,1,3,5];
  4.  
  5. // console.log(arr.sort()[0]);               // min
  6. // console.log(arr.sort()[arr.length -1]);     //max
  7.  
  8.  
  9. var min = arr1.sort()[0];
  10. var max = arr1.sort()[arr.length -1];
  11.  
  12. console.log(arr)
  13. // console.log(min,max);
  14.  
  15. var currMin = min;
  16. var currMax = max;
  17.  
  18.  
  19. console.log(currMin,currMax);
  20.  
  21.  
  22. //////// ALGORITMO
  23.  
  24. if (min > 1) {
  25.     console.log(1,"fine");
  26.     return;
  27. }
  28.  
  29. console.log(min,max);
  30.  
  31. var searched = min + 1;
  32.  
  33.  
  34. for (var i = 0; i < arr.length; i++) {
  35.     if (arr[i] > 0 ) {
  36.  
  37.         console.log(currMin, currMax,searched);
  38.         if (arr[i] > searched) {
  39.            
  40.             if (arr[i] < currMin) {
  41.                 currMin = arr[i];
  42.             }
  43.  
  44.             else if (arr[i] < currMax) {
  45.                 currMax = arr[i];
  46.             }
  47.  
  48.  
  49.         }
  50.  
  51.         else if (arr[i] == searched) {
  52.             currMin = searched;
  53.             searched++;
  54.         }
  55.  
  56.         else {
  57.            
  58.             if (arr[i] < currMax && arr[i] > searched) {
  59.                 currMax = arr[i];
  60.             }
  61.         }
  62.  
  63.        
  64.     }
  65. }
  66.  
  67.  
  68. if (searched == currMax) {
  69.     console.log(currMax+1, "through max");
  70. }
  71. else {
  72.     console.log(searched, "through searched");
  73. }
RAW Paste Data