Added a suggestion of solution with a simple for loop (#35884)

* Added a suggestion of solution

* fix: removed repl.it links and corrected bullets
pull/36341/head
Tiago Salema 2019-06-28 01:52:00 +01:00 committed by Randell Dawson
parent 0c4dce8984
commit fcefb7895d
1 changed files with 38 additions and 1 deletions

View File

@ -151,7 +151,7 @@ Note: If the array only has two elements, then the `for` loop never gets used an
* <a href='http://forum.freecodecamp.com/t/javascript-math-max/14682' target='_blank' rel='nofollow'>JS Math Max</a>
* <a href='http://forum.freecodecamp.com/t/javascript-math-min/14684' target='_blank' rel='nofollow'>JS Math Min</a>
## ![:rotating_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/rotating_light.png?v=3 ":rotating_light:") Advanced Code Solution:
## ![:rotating_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/rotating_light.png?v=3 ":rotating_light:") Advanced Code Solution 1:
function smallestCommons(arr) {
// Euclidean algorithm for the greatest common divisor.
@ -187,6 +187,43 @@ Note: If the array only has two elements, then the `for` loop never gets used an
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment' target='_blank' rel='nofollow'>Prefix increment operator ++</a>
## ![:rotating_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/rotating_light.png?v=3 ":rotating_light:") Advanced Code Solution 2:
const smallestCommons = arr => {
let max = Math.max(...arr);
let min = Math.min(...arr);
// Initially the solution is assigned to the highest value of the array
let sol = max;
for (let i = max - 1; i >= min; i--) {
// Each time the solution checks (i.e. sol%i===0) it won't be necessary
// to increment 'max' to our solution and restart the loop
if (sol % i) {
sol += max;
i = max;
}
}
return sol;
};
// test here
smallestCommons([1,5]);
### Code Explanation:
* Extract `min` and `max` from `arr` using `Math.min()` and `Math.max()`, respectively. As the arguments to these functions are integers, it is necessary to spread `...` the array.
* As a first guess, let's say that the solution is `max`. (we will increment this value later on if it is not the solution)
* Confirm that our solution is a multiple of all the values between `max` and `min` using a `for` loop.
* In case it isn't a solution, increment `max` to our solution (i.e. get the next multiple of the `arr` highest value) and restart the loop `i = max`. Note that it isn't `i = max - 1` since the `for` loop hasn't finished yet. Once it is finished, the loop itself will execute `i--`. It is also worth mentioning now that we started the loop at `i = max` and decremented `i` throughout instead of starting at `i = min` and then increment it in order to minimize the number of iterations.
* The `if` statement never being true means that all numbers between `min` and `max` are divisible by our solution
#### Relevant Links
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement_(--)' target='_blank' rel='nofollow'>Prefix decrement operator (--)</a>
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder_()' target='_blank' rel='nofollow'>Remainder operator (%)</a>
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min' target='_blank' rel='nofollow'>Math.min()</a>
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max' target='_blank' rel='nofollow'>Math.max()</a>
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax' target='_blank' rel='nofollow'>Spread syntax (...)</a>
## ![:clipboard:](https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:") NOTES FOR CONTRIBUTIONS: