Javascript Freecodecamp Algorithm #14: Smallest Common Multiple

preview_player
Показать описание
Learn how to solve freecodecamp javascript algorithms in various ways! This series is up-to-date with all ES6 and beyond javascript notations
Рекомендации по теме
Комментарии
Автор

Two years old and still useful. You make an excellent tutor

marlesyk
Автор

Love from my heart for you, Finally I understand the SCM concept by this video. Thank you so much!

mdminhaj
Автор

Thank you for the video, great explanation!

foodfairyshi
Автор

Thanks for the awesome explanation as always, one additional optimization would be to increment by multiples of the largest number instead of by 1 after each loop. Since the lowest common multiple must be a multiple of the largest number anyways. Instead of looping (5, 6, 7, 8) you loop (5, 10, 15, 20)... until you reach the answer.

af
Автор

amazing, slow and great explaination 👍

raihan
Автор

This is great for me, also the refactor at the end is really great. Thanks for helping me undertand it. Regards!

Aggroholic
Автор

alternate solution: function smallestCommons(arr) {
let start = arr[0] < arr[1] ? arr[0] : arr[1];
let end = arr[0] > arr[1] ? arr[0] : arr[1];
let lcm = Math.max(start, end);
for (let i = start; i <= end; i++) {
while(lcm % i !== 0){
lcm++;
i = start;
}
}
return lcm;
}




smallestCommons([1, 5]);

howardkim
Автор

this challenge has got really confusied, thanks for the video really simplly explaned but i just can't wrap my head around it yet .

Flame-on-fp
Автор

Thank you for your helpful video. I have a question: why isn't the 'while (true)' loop an infinite loop?

maxellcdr
Автор

So the while loop is "looping" as long as the condition inside of it is true, regardless of the condition given to it, otherwise if the code inside of the while loop happens to evaluate to "false" it stops? That's something so simple, yet something that I've easily overlooked. Every time I've used a while loop I've been so used to writing some sort of condition inside of it such as: While (smallNumber < biggerNumber) { some code until it evaluates to false (usually smallNumber++) }. But I actually never considered writing out a blatant while loop with (true) inside and breaking it if some unrelated false condition === false.

inactiveaccount
Автор

Nice video. i think there is a possible for an infinite loop at while(true)?

ikonxTech