Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Last active November 9, 2025 17:26
Show Gist options
  • Select an option

  • Save tatsuyax25/a030b1b4caed085da919731212d8588c to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/a030b1b4caed085da919731212d8588c to your computer and use it in GitHub Desktop.
You are given two non-negative integers num1 and num2. In one operation, if num1 >= num2, you must subtract num2 from num1, otherwise subtract num1 from num2. For example, if num1 = 5 and num2 = 4, subtract num2 from num1, thus obtaining num1 = 1 a
/**
* @param {number} num1
* @param {number} num2
* @return {number}
*/
var countOperations = function(num1, num2) {
// initialize a counter to keep track of the number of operations
let operations = 0;
// Continue looping until either num1 or num2 becomes 0
while (num1 !== 0 && num2 !== 0) {
// If num1 is greater than or equal to num2, subtract num2 from num1
if (num1 >= num2) {
num1 -= num2;
} else {
// Otherwise, subtract num1 from num2
num2 -= num1;
}
// Increment the operation counter
operations++;
}
// Return the total number of operations performed
return operations;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment