Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 求最小倍数_Javascript_Logic - Fatal编程技术网

Javascript 求最小倍数

Javascript 求最小倍数,javascript,logic,Javascript,Logic,我正在尝试自己学习和练习javascript,我已经取得了一些进步,但我仍然会遇到一些基本问题。我认为我的代码非常接近于给出正确的答案,但我看不出我遗漏了什么问题。如果有人在编码更好,请花一秒钟,并填写我的逻辑错误,我会非常感激 <script> //2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. v

我正在尝试自己学习和练习javascript,我已经取得了一些进步,但我仍然会遇到一些基本问题。我认为我的代码非常接近于给出正确的答案,但我看不出我遗漏了什么问题。如果有人在编码更好,请花一秒钟,并填写我的逻辑错误,我会非常感激

<script>

//2520 is the smallest number that can be divided by each of the numbers from 1 to         10 without any remainder.

var input = 11;

function smallestMultiple(){
    for(var i = 2; i <= 10; i++){
        if(input % i === 0 && isDivisible(input))
            alert(input);//this should only alert when input is divisible by all numbers between 2 and 10   
        }else{
            input ++;
//if input isn't divisible by all numbers between 2 and 10, increment input by 1        
        }
    }
};

// The following function should return true when "input" is divisible by 10, which is the trigger for alerting "input"

function isDivisible(input){

    if(input % 10 === 0){

        return true;

    }else{

        return false;

    }
};

smallestMultiple();



</script>

//2520是最小的数字,可以被1到10之间的每一个数字除,没有任何余数。
var输入=11;
函数smallestMultiple(){

对于(var i=2;i您的脚本将在
2,3,…,9
中找到可被10和整除的最小整数

更快的实现可能是设置几个临时除数,直到左边缘超过右边缘为止

最简单的方法是在
1,2,3…,9
中选择一个数字,然后尝试将其除以
1,2,3…,9

下面的html沙盒(保留主题)可能会帮助您理解

<html>
<head>
    <meta charset="utf8">
</head>
<body>

    <div>
        <p class="output"></p>
    </div>

    <script>
    window.onload = function() {

        function smallest_shared_multiple(from, to) {
            var tmp_divisor = from
            var tmp_candidate = tmp_divisor

            for(;tmp_divisor < to +1;) {
                if (tmp_candidate % tmp_divisor) {
                    tmp_divisor = from
                    tmp_candidate++
                } else {
                    tmp_divisor++
                }
            }

            return tmp_candidate                                       
        }

        document.querySelector('p.output').innerHTML =
            'For the given range, the smallest shared multiple is ' +
            smallest_shared_multiple(1, 10)

    }
    </script>
</body>
</html>

window.onload=函数(){ 函数最小共享倍数(从,到){ var tmp_除数=从 var tmp_候选者=tmp_除数 对于(;tmp_除数<到+1;){ if(tmp_候选%tmp_除数){ tmp_除数=从 tmp_候选人++ }否则{ tmp_因子++ } } 返回tmp_候选人 } document.querySelector('p.output').innerHTML= '对于给定范围,最小共享倍数为'+ 最小共享倍数(1,10) }

<> > >编辑< /强>:请考虑在发布前缩进代码。此外,作为编程的一般规则,更好的命名函数的方式是唤起他们应该做的……并且使变量的范围最小。

你可以考虑更好地格式化你的代码。它会使你更容易识别问题。你能提供一些关于如何使问题变得更好的建议吗?确保所有的东西都被正确地缩进。把你的代码扔进去看看有什么好的缩进。谢谢你的帮助。我不理解你的“to”的语法。循环。分号中有“for”循环参数是什么意思?类C的
for
循环的语法是
for(初始化内容;要维护的条件;每次迭代时要执行的指令)
。如果不需要,可以省略这三个块中的某些块。在这种情况下,我们只需要检查在每个循环中是否保持了该条件。这是另一种(可能是复杂的)编写
的方法,而
循环。