WML - Overview Advertisements. Previous Page. Next Page. Previous Page Print Page. Save Close. The only part of this which changes in this self-modfying program is decrementing the multiplier. To decrement the multipler, it needs to fetch what it is currently, decrement it, and copy it back in. Self-modifying programs are interesting. Sign up.
Already have an account? Click here to login. Set a password. New Password. Resend Verification Code. Set Password. Reset password. Verification Code. Get Reset Password Code Already have an account? Privacy Policy. Report Answer. Connect and share knowledge within a single location that is structured and easy to search.
I have the following code but it is not giving perfect result for factorial can u find it out plz. How to make it work for negative numbers I leave up to you ; but I showed in this post anyway. Using ternary operator we replace the above code in a single line of code as below.
Here I used ES6 arrow function. For better understanding please see what is arrow function. Recursion in JS is open to stack overflow error and also very slow. Looping by other means is better. My contribution to factorial code would be a straightforward one;. Try adding return x; to the bottom of your function. I am not sure why no one used dynamic programming to answer this, it's by far the most efficient way to build something on a factorial in my view.
The code above first defines two variables, factorialNumber and factorial. When successfully calculated, we show the result using alert. You first set a table to act as a way to input your variable and have a place to output the answer. You also add a button to execute your function.
The input variable is the value entered by the user. You also have int variable as a placeholder. Inside the do loop you then another variable that is the product, it takes your placeholder variable and times it by the input. After this the input decrements, as long as input value is then greater than zero the loop keeps iterating.
I've seen a recursive approach used in many places Eloquent JavaScript etc. Here, the function is called recursively until it reaches 0, which should not be the case.
It's a very minor change, and probably does not matter. Curious to know what other people think of it. Assuming it is a valid positive integer. How are we doing? Please help us improve Stack Overflow.
Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
0コメント