用Javascript编写组合和置换计算器的最有效方法 [英] Most efficient way to write Combination and Permutation calculator in Javascript

查看:42
本文介绍了用Javascript编写组合和置换计算器的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数学网站 http://finitehelp.com ,该网站教授学生有限数学.我认为包括一个计算器会很酷,所以我用Java编写了一个用于组合和排列的计算器.实时计算器位于 http://finitehelp.com/finite-calculator.html .我对Javascript几乎一无所知,并且会冒险猜测有一种更高效的方法来编写以下代码,尤其是因为过度使用了变量.如果有人可以帮助我,我将非常感激.

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>

推荐答案

好,我们开始!

首先,为什么还要编写这个?

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

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

我会完全消除它.

您还可以稍微整理一下 Math.factorial :

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));
    }
}

但是主要问题是您的 onclick()代码:

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;

这太复杂了.我会使其成为一个函数并将其绑定到元素,这将消除HTML中的所有废话,并使其更易于使用:

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));
    }
}

然后删除 onclick = 代码.

这篇关于用Javascript编写组合和置换计算器的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆