Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function smallestMult(n) {
- let j = 0;
- let counter = 0;
- let bool = true;
- let result = 0;
- do {
- j++;
- counter = 0;
- for (let i = 1; i <= n; i++) {
- if (j % i !== 0) {
- counter++;
- break;
- }
- }
- if (counter === 0) {
- bool = false;
- result = j;
- }
- } while (bool);
- return result;
- }
- console.log(smallestMult(20));
- function smallestMult(n) {
- let counter = 0;
- let bool = true;
- let result = 0;
- //Here for sure do not use Infinity !
- //Better use a Boolean that you control instead
- for (let i = 1; bool; i++) {
- counter = 0;
- for (let j = 1; j <= n; j++) {
- if (i % j !== 0) {
- counter++;
- break;
- }
- }
- if (counter === 0) {
- //It is bad idea to have return in an infinite loop!
- // use variable that have initiated outside the loops to save the i
- // then set the loop Boolean var to false to stop the big loop
- result = i;
- bool = false;
- }
- }
- //Return the var that you saved the i value inside the loop
- return result;
- }
- //General NOTE
- //If this does not work try using do while loop this more fits the task
- // do this and that until you find a good value
- // If this does not work contact me again
- // In general always try to use varuables inside the loop that are accebila outside the loop and controll them instead of using return
- // in a loop this is not agood practice
- console.log(smallestMult(20));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement