Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6683607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:50:29+00:00 2026-05-26T04:50:29+00:00

I have a math website http://finitehelp.com that teaches students Finite Math. I thought it

  • 0

I have a math website http://finitehelp.com that teaches students Finite Math.
I thought it would be cool to include a calculator so I made one for combinations and permutations in Javascript. Live calculator is at http://finitehelp.com/finite-calculator.html. I know next to nothing about Javascript and would venture to guess there is a much more efficient way to write the following particularly because of the excessive use of variables. If someone could please help me I would be very grateful.

<script type="text/javascript">
// calculate n!
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));
}
Math.divide = function(a,b)
{
    return a/b;
}
</script>

<form class="form" name="combination" action="">
    <p>C(<input type="text" value="n" name="T1" size="1">,<input type="text" value="r" name="T2" size="1">)
    <input type="button" value="Calculate"
     onclick="var n = T1.value; var r = T2.value; var n_minus_r = parseFloat(n) - parseFloat(r); var numerator = Math.factorial(T1.value); var n_minus_r_fact = Math.factorial(n_minus_r); var r_fact = Math.factorial(r); var denominator = n_minus_r_fact * r_fact; T3.value = Math.divide(numerator,denominator); return true;">
    = <input type="text" name="T3" size="12" readonly></p>
</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:50:29+00:00Added an answer on May 26, 2026 at 4:50 am

    Well, here we go!

    First of all, why would you ever need to write this?

    Math.divide = function(a,b)
    {
        return a/b;
    }
    

    I would do away with it completely.

    You can also clean up your Math.factorial a little bit:

    Math.factorial = function(n)
    {
        n = Number(n);
    
        if (isNAN(n)) {
            alert("Factorial requires a numeric argument.");
            return null;
        } else if (n < 2) {
            return 1;
        } else {
            return (n * Math.factorial(n - 1));
        }
    }
    

    But the main problem is your onclick() code:

    onclick="var n = T1.value; var r = T2.value; var n_minus_r = parseFloat(n) - parseFloat(r); var numerator = Math.factorial(T1.value); var n_minus_r_fact = Math.factorial(n_minus_r); var r_fact = Math.factorial(r); var denominator = n_minus_r_fact * r_fact; T3.value = Math.divide(numerator,denominator); return true;
    

    This is way too complicated. I’d make it a function and bind it to the element, which would get rid of all of the crap in your HTML and make it a bit easier to work with:

    window.onload = function()
    {
        document.getElementById('calculate').onclick = function() {
            var n = T1.value,
                r = T2.value;
    
            T3.value = Math.factorial(n) / (Math.factorial(r) * Math.factorial(n - r));
        }
    }
    

    And just get rid of the onclick= code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written something that uses the following includes: #include <math.h> #include <time.h> #include
i have some xml with math ml inside, and i would like to find
I have a method that accepts an IEnumerable-decimals and performance various math functions. I
I'm creating a website for my schools Math Relay competition. I have a Container
I have a little math problem. I would like to have a function with
So this source code comes free from this website. http://math.hws.edu/javamath/basic_applets/SliderGraph.html It seems to be
Lets say you have website that keeps balance for each user. You give each
We have a website with many math formulas (displayed as pngs, converted from Latex)
I have a math problem that I solve by trial and error (I think
I have just finished building a website for client, instantinteriors.com.au . (Visit this site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.