Question: Write a function lcm that takes two natural numbers and calculates their least common multiple (lcm). The lcm of two natural numbers a und b is the smallest natural number that is divisible by a and b. Example: lcm(4, 6) should return 12. Answer: function lcm(a, b) { let theLCM = 0; let remainderA; let remainderB; do { theLCM++; remainderA = theLCM % a; remainderB = theLCM % b; } while (remainderA !== 0 || remainderB !== 0) return theLCM; } > we declare a function lcm > it has 2 parameters: 'a' and 'b' > we declare a variable 'theLCM' & initialize it with a value of 0; > we declare 2 empty variables: 'remainderA' & 'remainderB' > we open a do while loop > do/while loop is a variant of the while loop > this loop will execute the code block once, before checking if the condition is true > then it will repeat the loop a...
Comments
Post a Comment