Advertisement
ErolKZ

Boris

Dec 17th, 2021
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1.  
  2. function smallestMult(n) {
  3.  
  4. let j = 0;
  5.  
  6. let counter = 0;
  7.  
  8. let bool = true;
  9.  
  10. let result = 0;
  11.  
  12.  
  13. do {
  14.  
  15. j++;
  16.  
  17. counter = 0;
  18.  
  19. for (let i = 1; i <= n; i++) {
  20.  
  21. if (j % i !== 0) {
  22.  
  23. counter++;
  24.  
  25. break;
  26.  
  27. }
  28.  
  29. }
  30.  
  31. if (counter === 0) {
  32.  
  33. bool = false;
  34.  
  35. result = j;
  36.  
  37. }
  38.  
  39.  
  40. } while (bool);
  41.  
  42. return result;
  43.  
  44. }
  45.  
  46.  
  47. console.log(smallestMult(20));
  48.  
  49.  
  50.  
  51. function smallestMult(n) {
  52.  
  53.  
  54. let counter = 0;
  55.  
  56. let bool = true;
  57.  
  58. let result = 0;
  59.  
  60. //Here for sure do not use Infinity !
  61. //Better use a Boolean that you control instead
  62.  
  63.  
  64. for (let i = 1; bool; i++) {
  65.  
  66. counter = 0;
  67.  
  68. for (let j = 1; j <= n; j++) {
  69.  
  70. if (i % j !== 0) {
  71.  
  72. counter++;
  73.  
  74. break;
  75.  
  76. }
  77.  
  78. }
  79.  
  80. if (counter === 0) {
  81.  
  82. //It is bad idea to have return in an infinite loop!
  83. // use variable that have initiated outside the loops to save the i
  84. // then set the loop Boolean var to false to stop the big loop
  85.  
  86. result = i;
  87.  
  88. bool = false;
  89.  
  90. }
  91.  
  92. }
  93.  
  94. //Return the var that you saved the i value inside the loop
  95.  
  96. return result;
  97.  
  98.  
  99. }
  100.  
  101. //General NOTE
  102. //If this does not work try using do while loop this more fits the task
  103. // do this and that until you find a good value
  104. // If this does not work contact me again
  105. // In general always try to use varuables inside the loop that are accebila outside the loop and controll them instead of using return
  106. // in a loop this is not agood practice
  107.  
  108. console.log(smallestMult(20));
  109.  
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement