
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Solution to the clumsy factorial problem in JavaScript
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.
We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order.
For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1. However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right.
Let's write the code for this function in JavaScript −
Example
const clumsy = num => { let k=num; let res = 0, temp=1, i=0; while(k>0){ temp = k; if(k-1>0){ temp*=(k-1); } if(k-2>0){ temp/=(k-2); } if(k-3>0){ if(k===num){ temp+=(k-3); }else{ temp-=(k-3); } } if(k===num){ res = temp; }else{ res = res-temp; } k = k-4; } return res; }; console.log(clumsy(4)); console.log(clumsy(10)); console.log(clumsy(16)); console.log(clumsy(5));
Output
The output in the console will be −
7 11.75 16.609523809523807 7.666666666666668
- Related Articles
- Clumsy Factorial in Python
- Solution for array reverse algorithm problem JavaScript
- Factorial recursion in JavaScript
- Encoding decimal to factorial and back in JavaScript
- Calculating factorial by recursion in JavaScript
- The algorithm problem - Backtracing pattern in JavaScript
- Function to compute factorial of a number in JavaScript
- Snail Trail Problem in JavaScript
- Recursive Staircase problem in JavaScript
- Distributing Bananas Problem in JavaScript
- How to write the factorial function with reduce and range in JavaScript?
- Solve the Sherlock and Array problem in JavaScript
- Calculating the sum of digits of factorial JavaScript
- Recursion problem Snail Trail in JavaScript
- Expressive words problem case in JavaScript
