Smallest Common Multiple - Intermediate Algorithm Scripting - Free Code Camp

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Honestly this one totally stumped me and even looking at the solutions really made me scratch my head, but you explained everything perfectly and helped a lot. Thank you dude, you're really helping people understand some tough problems in an easy to follow way and that's no easy feat with some of these problems! 🙌

nicholasrobbins
Автор

I need to learn a lot from you. I spent around 80 hours in this problem and I couldn`t find the answer. It was the same thing Where art thou. Thanks for teaching.

aaronargottelopez
Автор

two months ago i went through all the challenges with your videos as a guide and it was very helpful. Thanks usefulprogrammer! I'm going through them again and came up with a solution using every:

function smallestCommons(arr) {
let fullArr = []
arr.sort((a, b) => {return a - b})
for(let i = arr[0]; i <= arr[1]; i++){
fullArr.push(i)
}

let incrementer = 1
while((fullArr.every(ele => (incrementer % ele) === 0)) === false){
incrementer++
}
return incrementer
}

adamknuds
Автор

Thank you very much for the videos they are very useful, here is my solution from another point of view:

function smallestCommons(arr) {
let higherN = Math.max(...arr); //gets the higher num in the array
let lowerN = Math.min(...arr); //same as above but lower num
let nums = []

for (let i = lowerN; i <= higherN; i++) { //create the array with all the numbers to verify
nums.push(i)
}

let divisor = higherN //create the variable to increment by the higher number

while (true) {
if (nums.every(item => divisor % item == 0)) { //if the divisor that is supposed to be always a higher number mod, verifying it with all the nums in the array
return divisor //and everyone returns true we found the solution and return it!
} else {
divisor += higherN //if not, divisor is incremented by the higher number, which always its a mod
}
}
}


smallestCommons([1, 5]);

skatepanama
Автор

Thank you. This solution really opened my mind to a new way of solving problems with arrays. Setting the array length equal to the count, I found to be quite eye opening and ingenious.

bassmw
Автор

Seems like not only I was stuck in this challenge)) when I reached the stage to find smallest Common Multiple after lining up sequence and creating an array, the rest was really strenuous. Tried many ways... and got lost))
I appreciate your work, thanks for tutorial

sarvarkhalimov
Автор

You're a saint for making these videos brother. Thank you

pokebeaches
Автор

function smallestCommons(arr) {
if (arr[0] > arr[1]) {
[arr[0], arr[1]] = [arr[1], arr[0]];
}
console.log(arr);
}

smallestCommons([5, 1]); // logs [1, 5]

I find this way of rearranging array items very easy when we have 2 items like in this case.
Thanks for the tutorials. They're awesome!

jovannikolic
Автор

Nice to see your thought process. A good example of thinking through the problem. I had got the part about proving all "true" myself but was getting "head fuzz" by then.

brianfitzsimmonsable
Автор

It's been more than 3 days, i was stuck with this problem and finally have been able to solve this.Thank you.

ShubhamMishra-mzzw
Автор

Very nice explanation of the problem and your solution. Simple but with room to optimise and learn.

quaidbergo
Автор

Thanks mate. Been struggling to follow other explanations. Seeing the steps in the log is extremely helpful. Was a bit confused about the math part. Great work man.

julianstorm
Автор

function smallestCommons(arr) {
const min = Math.min(...arr)
const max = Math.max(...arr)
let accumulator = max
for (let i = max; i >= min; i--){
if(accumulator % i != 0){
accumulator += max;
i = max;
}}
return accumulator;
}

Is the smallest version that works that I've seen. After spending a few hours on this I was surprised to find this on the comments of the solution. Surprisingly simple

koddezign
Автор

That was no fckin way Intermediate Level. That was brootal

mindrust
Автор

yeah i always check the hints, just to try to understand them, i think is very usefull

aaaamontenegro
Автор

It took me a few days, but I got it on my own.. Man the relief just but I'm just here looking at how other people did it.

cristiancarrillocastillo
Автор

Thanks again for the video. For anyone wanting to use this code to pass all hypothetical tests, instead of using a large number in the while loop you can create a variable x=true and change it to x=false once the counter reaches the max value (arr.length).

JService
Автор

instead of multiple < or whatever upperbound

just do
while (multiple)
if you found the answer set the multiple into undefined 🤯

theMiaow
Автор

thank you very much! it's really helping me to understand this problem.

RusydyMuhiddin
Автор

Tricky lesson Mr. Ian but thanks for not only explaining it in a way I can understood but thanks for moving slowly with the lesson that I can see what I did correct and what I did not do correct. Here is a easier way to get the correct answer as well down below:
function smallestCommons(arr) {
let max = Math.max(...arr);
let min = Math.min(...arr);
let sol = max;
for (let i = max - 1; i>=min; i --) {
if (sol % i !== 0) {
sol += max;
i = max;
}
}
return sol;
}
console.log(smallestCommons([23, 18])); and your answer will then come to be 6056820.

zken