以下代码是我的原始代码,即我在不复制其他代码或观看vedios执行此功能的情况下编写此代码。所以我的代码所做的是,它将取出任何数字的LCM:
的javascript
var number1_factors = [];
var LCM = function(){
number1_factors.length = 0;
var number1 = document.getElementById("in1").value;
var number1_array = number1.split('');
var sum = 0;
for (var i = 0; i < number1_array.length; i++) {
sum += parseInt(number1_array[i],10);
};
if(number1 % 2 === 0){
for(number1 ; number1 % 2 == 0 ; )
{
number1_factors.push(2);
number1 = number1 / 2;
}
};
if(sum % 3 === 0){
for(number1 ; number1 % 3 == 0 ; )
{
number1_factors.push(3);
number1 = number1 / 3;
}
};
if ( number1 % 5 === 0 ){
for (number1 ; number1 % 5 == 0;){
number1_factors.push(5);
number1 = number1 / 5;
}
};
if( number1 % 7 === 0 ){
for (number1 ; number1 % 7 == 0;){
number1_factors.push(7);
number1 = number1 / 7;
}
}else{
number1_factors.push(parseInt(number1));
};
}
HTML
<input type="text" id="in1" />
<input type="text" id="in2" />
<button onclick="LCM();">LCM</button>
<div id="result"></div>
这段代码运行完美,但你知道这个函数太大了,所以我需要简化这段代码。有什么办法可以减少这段代码的大小吗?
每一次消化都会感谢你们。