Javascript 我应该如何将if-else条件的代码管理为其他条件?
我有三种方法,例如:Javascript 我应该如何将if-else条件的代码管理为其他条件?,javascript,loops,Javascript,Loops,我有三种方法,例如: function method1(){ console.log('method1'); } function method2(){ console.log('method2'); } function method3(){ console.log('method3'); } 现在我将在if-else条件下应用这些方法,如下所示: for(var i=0;i<100;i++){ //for first 4 iteration apply met
function method1(){
console.log('method1');
}
function method2(){
console.log('method2');
}
function method3(){
console.log('method3');
}
现在我将在if-else条件下应用这些方法,如下所示:
for(var i=0;i<100;i++){
//for first 4 iteration apply method1:
if(i<4){
method1();
}
//next fifth iteration apply method2:
else if(i==4){
method2();
}
//next 3 iteration apply method3:
else if(i>4 && i<8){
method3();
}
//next ninth iteration apply method2:
else if(i==8){
method2();
}
//next 3 iteration apply method1:
else if(i>8 && i<12){
method1();
}
...........
}
for(var i=0;i)在这些条件下,我会这样做:
我会去好的旧excel,在1列是迭代器I,在另一列是我想运行的方法
然后我会编写一个宏来自动生成代码来复制/粘贴它。这需要时间,但如果这是可重用的,excel将成为您的设计和文档。我认为您的程序遵循这种模式
for (var i = 0; i < 100; i += 1) {
if (i % 4 === 0 && i) {
method2(); // Current index is divisible by 4
} else if (i % 8 < 4) {
method1(); // Current index is within 0-3, 8-11, ...
} else {
method3(); // Current index is within 5-7, 13-15, ...
}
}
for(变量i=0;i<100;i+=1){
如果(i%4==0&&i){
method2();//当前索引可被4整除
}否则如果(i%8<4){
method1();//当前索引在0-3,8-11。。。
}否则{
method3();//当前索引在5-7,13-15。。。
}
}
您可以按照以下模式编写代码,然后解决数学问题
var foo = {
method1: function(){
console.log('method1');
},
method2: function(){
console.log('method2');
},
method3: function(){
console.log('method3');
}
}
var bar = function (num) {
// put your formular here
return num;
}
for(var i=0; i<100; i++){
foo['method'+bar(i)]();
}
var foo={
方法1:函数(){
console.log('method1');
},
方法2:函数(){
console.log('method2');
},
方法3:函数(){
console.log('method3');
}
}
变量条=函数(num){
//把你的公式放在这里
返回num;
}
for(var i=0;ifor(var i=0;i<100;i++){
如果(i>0&&i%4==0){
方法2();
}
else if(parseInt(i/4)%2==0){
方法1();
}
否则{
方法3();
}
}
看看这个!您的图表显示,最初只有method1执行4次(0到3之间)。之后,method3和method1的模式各执行3次,但被method2(每4次)隔开。如果在本系列结束之前都是这种情况,那么下面的代码将起作用-
if (nCtr < 4) {
method1();
} else if ((nCtr % 4) == 0) {
method2();
} else if (((nCtr / 4) % 2) == 0) { // nCtr divided by 4 produces even number
Method1();
} else { // nCtr divided by 4 produces odd number
Method3();
}
if(nCtr<4){
方法1();
}如果((nCtr%4)==0),则为else{
方法2();
}如果((nCtr/4)%2)==0{//nCtr除以4,则生成偶数
方法1();
}否则{//nCtr除以4产生奇数
方法3();
}
我没有足够的时间来发布一个完整的答案,但是考虑创建一个在每个迭代开始时用一个<代码>开关、CASE < /COD>子句来解决的动态案例集。它应该为您的问题提供一个干净的和功能性的解决方案。这是一个有趣的设计模式,但我有点困惑:在现实世界中,这种模式是否被应用?在我看来,这真的是一个“数学”问题。坐下来,花点时间真正理解什么时候应该使用哪种方法。写出一个公式。只有在绝对必要的时候才破例。你在这n业务上走的是正确的道路,但我不认为你的公式正确。小测验:将此答案与进行比较,找出差异;)op说,对于前4次迭代,apply method1
,当i为0时,您的代码会中断。它的工作原理几乎和我的一样好,但0%4=0,因此,在这种情况下,您的答案应该调用method2。但是如果您解决了这个问题,您的答案是正确的;D@Buzinas实际上,我不是很确定。根据模式,它应该调用method2
仅限,无论如何我已修复:)@xdazz请现在检查,我已修复,我非常喜欢你的方法。但是你说的是把公式放在bar函数中。你能给我看一个演示吗?因为你比其他人更喜欢他的答案,所以我编辑了它,把数学公式放在里面。现在,我们只需要等待它得到批准=)你能提供详细的答案吗。
var foo = {
method1: function(){
console.log('method1');
},
method2: function(){
console.log('method2');
},
method3: function(){
console.log('method3');
}
}
var bar = function (num) {
// put your formular here
return num;
}
for(var i=0; i<100; i++){
foo['method'+bar(i)]();
}
for (var i = 0; i < 100; i++) {
if (i > 0 && i % 4 == 0) {
method2();
}
else if (parseInt(i / 4) % 2 == 0) {
method1();
}
else {
method3();
}
}
if (nCtr < 4) {
method1();
} else if ((nCtr % 4) == 0) {
method2();
} else if (((nCtr / 4) % 2) == 0) { // nCtr divided by 4 produces even number
Method1();
} else { // nCtr divided by 4 produces odd number
Method3();
}