davegimo

minpositive

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