- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are required to write a JavaScript function that takes in two numbers, m and n, as the first and the second argument.

Our function is supposed to count the number of minimum operations required to reach n from m, using only these two operations −

**Double**− Multiply the number on the display by 2, or;**Decrement**− Subtract 1 from the number on the display.

For example, if the input to the function is −

const m = 5; const n = 8;

Then the output should be −

const output = 8;

Because the operations are −

5 → 4 → 8

The code for this will be −

const m = 5; const n = 8; const findOperations = (m, n) => { let res = 0; while(n > m){ if(n % 2 === 0){ n /= 2; }else{ n += 1; }; res += 1; }; return res + m - n; }; console.log(findOperations(m, n));

And the output in the console will be −

2

- Related Questions & Answers
- Find the minimum number of steps to reach M from N in C++
- Convert a number m to n using minimum number of given operations in C++
- Program to find number of given operations required to reach Target in Python
- Program to find minimum number of hops required to reach end position in Python
- Program to find minimum number of buses required to reach final target in python
- Sum of even numbers from n to m regardless if n<m or n>m JavaScript
- Minimum number of operations required to sum to binary string S using C++.
- Program to find minimum number of operations required to make one number to another in Python
- Minimum number of given operations required to make two strings equal using C++.
- Minimum number of operations required to delete all elements of the array using C++.
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Minimum operations required to remove an array in C++
- Program to find minimum number of operations required to make one string substring of other in Python
- Find minimum steps required to reach the end of a matrix in C++

Advertisements