/    Sign up×
Community /Pin to ProfileBookmark

factorial numbers in java script

[B]hey guys, i have a problem while writing factorial calculations in java script.. the calculation im looking to answer is :

z = x! / (x-y)!

the following is my code so far: [/B]

<html>

function factorial(n) {
if ((n == 0) || (n == 1))
return 1
else {
var result = (n * factorial(n-1) );
return result
}
}

<script language = “javascript”>

<!–

var x = 0;
var y = 0;
var z = 0;

x = window.prompt(“Enter first number..”);
x = parseInt(x);

y = window.prompt(“Enter second number..”);
y = parseInt(y);

z = (factorial x / factorial (x-y));

alert(“THE ANSWER IS ” + z);

//–>

</script>

<body>

</body>

[B]any comments would be great..[/B]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@purefanDec 09.2005 — 
  • 1. Posting using the code tags makes us readers more willing to help

  • 2. I would put the function factorial [B]in[/B] the script.

  • 3. What error are you getting?
  • Copy linkTweet thisAlerts:
    @purefanDec 09.2005 — I havent gone through all of your code but this is one detail that I noticed:

    [code=html]z = (factorial(x) / factorial (x-y));[/code]
    instead of
    z = ([B]factorial x[/B] / factorial (x-y));[/QUOTE]
    Copy linkTweet thisAlerts:
    @senioretaNov 18.2008 — hi Patrick,

    u have not right the following like this:

    var x = 0;

    var y = 0;

    var z = 0;

    u have to put comma (,) after each statement exept the last one (?

    so it must look like this:

    var x = 0,

    var y = 0,

    var z = 0;


    and in the propmt it should look like:

    window.prompt("Enter first number", "0");


    that what i notice so far

    i hope that i help u cuz am not sure 100&#37;

    good luck
    Copy linkTweet thisAlerts:
    @senioretaNov 18.2008 — hi Patrick,

    u have not right the following like this:

    var x = 0;

    var y = 0;

    var z = 0;

    u have to put comma (,) after each statement exept the last one (?

    so it must look like this:

    var x = 0, y = 0, z = 0;


    and in the propmt it should look like:

    window.prompt("Enter first number", "0");


    that what i notice so far

    i hope that i help u cuz am not sure 100%

    good luck
    Copy linkTweet thisAlerts:
    @MrNobodyNov 18.2008 — I used this function:
    [CODE]Math.factorial = function(n)
    {
    if(typeof n == 'string') n = Number(n);
    if(typeof n != 'number' || isNaN(n))
    {
    alert("Factorial requires a numeric argument.");
    return null;
    }
    if (n < 2) return 1;
    return (n * Math.factorial(n-1));
    }[/CODE]

    and this to test it:
    [code=html]<form action="">
    <p><input type="text" name="T1" size="6">
    <input type="button" value="Factorial"
    onclick="T2.value = Math.factorial(T1.value); return true;">
    = <input type="text" name="T2" size="12" readonly></p>
    </form>[/code]

    Otherwise, as pointed out, the syntax here is incorrect:
    z = (factorial x / factorial (x-y));
    ×

    Success!

    Help @patrick_wolf spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...