The code I wrote seems to be working but the “limit” on the console (infinite loop) make me unable to get the write answer for smallestCommons([23, 18]).
Since I know there is no infinite loop even for this last function call I was wondering if there was a way to “remove” this limit.
I’m sure there there is an easier way to solve this challenge but since I came up with this solution, I don’t really want to start again from scratch.
Thank you everyone !
**Your code so far**
function smallestCommons(arr) {
//sort the array :
let arrTwo = [...arr];
arrTwo.sort(function(a, b) { return a - b });
// select the the number from which I'll start :
let goodNum = arrTwo[1] + 1 ;
// each number between the range in the array is replaced inside :
let totalNum = (arrTwo[1] - arrTwo[0]) - 1;
let i = 0;
while(totalNum > 0) {
arrTwo.splice(i + 1, 0, arrTwo[i] + 1);
i++;
totalNum--;
}
// check every number starting with the good num. If the "goodNum" is wrong, we increase it and start the loop again (i = 0) :
for (let i = 0; i < arrTwo.length; i++) {
if(!Number.isInteger(goodNum / arrTwo[i])) {
goodNum++;
i = 0 ;
}
}
console.log(goodNum);
return goodNum;
}
smallestCommons([5, 1]);
smallestCommons([23, 18]);
smallestCommons([1, 13]);
smallestCommons([2, 10]);
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36.
There’s only two values in an passed array. Using sort is an overkill and confuses the reader. Just use a simple Math.max or an if test. It makes the code a lot more clearer.
You are resetting the counter variable i inside the for loop. Attempting to change a counter variable inside a loop is a big NO NO, because it is dangerous. Use a Boolean variable and a while loop. Use a for loop for fixed-sized repetition.
(Note: This is my personal opinion. Please take it as such.) I can understand your feeling that you invested time so you don’t want to come with another solution from scratch. I don’t want to sound harsh or negative, but that’s really is not what we seek from software developers. If you have an unnecessarily complex code and keep on debugging it, you could end up in a deeper hole. We want a simple, elegant, easy-to-understand, and efficient code from developers. It is often far more quicker to come up with a working correct code if you start from scratch.
Your code was throwing off an alarm for an infinite loop because on the [18,23] test it was to inefficient, and it could not solve your problem. FCC was actually breaking your loop so it was returning an answer, but it was wrong as it never got to the right answer. You don’t need to wipe your code you just need to refactor it some to make it work, and you don’t need to even change it that much.
It would be efficient to increment goodNum by the highest number rather than by 1, and this works because you know its going to be a multiple of the highest number, also goodNum should start as the highest number for this to work rather than the highest number plus one.
The way that you created your range is a bit over complicated, and you can achieve the same thing by saying push from the lowest to the highest number.
Sometimes you should just wipe out your code, but you do not need to do that, just refactor it some, or do wipe it out because that is fine.
I got your code passing by making 3 minor changes, if you have any questions feel free to ask.
Thank you for you answer, I understand your point of view. But I feel like changing the code actually helps me improving instead of just deleting it to start again.
But I’ll remember your advise, keeping a code working is important but doing so in a efficient way instead of debugging it is even better.
@caryaharper Thank you as well. It’s good to know that I wasn’t so far from the answer after all. I’ll look at it again and change a few things following your advise.
Worst case, I delete it. Thank you again !
Thanks for replying back to me. Yes, of course, for sake of learning the language and algorithm, continue working on the code is fine. What I tried to convey is about my general philosophy of taking a breath, step back, and rethink the problem if code gets complicated and having hard time fixing it. Often times, once you have a better and clearer approach, you can in fact reuse parts of the code already written.