Problem Description: Write a function that calculates the least common multiple of its arguments; each argument is assumed to be a non-negative integer.
We can find LCM of two numbers by a method which we learnt at high school but finding LCMs of n number of arguments can become a tiresome process through that method. Here our Javascript function takes n number of arguments and we have to find their LCM. We start by pushing our input arguments to an input array. Then we declare a variable L and initialize it to the value of LCM of first 2 numbers of input array. We then loop through the complete array and continue finding LCM of consecutive numbers.
In the end we return the value of L as the answer to this Kata. Keep in mind that this Kata can also be solved through recursion.
Please mention your suggestions or your doubts in the comment below.
Happy coding. :)
No comments:
Post a Comment