"超出最大呼叫堆栈大小"从函数内部调用函数

时间:2017-10-18 11:18:32

标签: javascript function loops if-statement

好的,所以我试着制作一个小型数独发生器,在遇到问题之前我没有走远。我遇到错误"超出最大调用堆栈大小"当console.log中的输出是" a == b"。这是我的代码(我不是经验丰富的编码器FUI)

function en_Til_Ni() {
    return Math.floor(Math.random()*9)+1;
} 

var enTilNi = en_Til_Ni(); 
console.log(enTilNi);

var a = en_Til_Ni();
console.log("a" + a);
var b = en_Til_Ni();
console.log("b" + b);
var c = en_Til_Ni();
console.log("c" + c);


console.log("---------------------------------------------");

function ikkeLike() { //this is where it goes wrong
    if (a != b) {
        console.log(a, b);
    }

    else if (a == b) { // It logs the numbers just fine, untill a == b
        ikkeLike();
    }

}

ikkeLike();

4 个答案:

答案 0 :(得分:1)

当然你得到这个结果,因为你没有工作退出条件。

您需要更改随机值并再次检查,直到达到所需状态。



function en_Til_Ni() {
    return Math.floor(Math.random() * 9) + 1;
}

function ikkeLike() {
    a = en_Til_Ni();
    b = en_Til_Ni();
    if (a !== b) {
        console.log(a, b);
    } else {              // no need for the opposite check
        ikkeLike();
    }
}

var a, b;                 // global variables
ikkeLike();




没有递归调用和do ... while循环的更好版本。



function en_Til_Ni() {
    return Math.floor(Math.random() * 9) + 1;
}

function ikkeLike() {
    do {
        a = en_Til_Ni();
        b = en_Til_Ni();
    } while (a === b)
    console.log(a, b);
}

var a, b; // global variables
ikkeLike();




答案 1 :(得分:0)

这对您的问题来说很难解决:

使用setTimeout来避免此错误消息,应用程序会看到无限循环问题。

您还需要为a和b创建新值!

function en_Til_Ni() {
    return Math.floor(Math.random()*9)+1;
} 

var enTilNi = en_Til_Ni(); 
console.log(enTilNi);

var a = en_Til_Ni();
console.log("a" + a);
var b = en_Til_Ni();
console.log("b" + b);
var c = en_Til_Ni();
console.log("c" + c);


console.log("---------------------------------------------");

function ikkeLike() { 

    if (a != b) {
        console.log(a, b);
    }

    else if (parseInt(a) == parseInt(b) ) { // It logs the numbers just fine, untill a == b
    
    console.log (" a == b  TRUE" ); 

    a = en_Til_Ni();
    b = en_Til_Ni();
    c = en_Til_Ni();

        setTimeout ( ikkeLike , 1 ) 
        console.log("cool")
        
    }

}

ikkeLike();

答案 2 :(得分:0)

  

好的,所以我试图制作一个小型数独发生器,但我没有得到   在我遇到问题之前。

您希望检查自己生成的三个值 - abc是否不相等。并且您希望继续生成bc的值,直到它们完全不同。

更改方法en_Til_Ni,使其始终生成随机值,直到值唯一。

function en_Til_Ni() 
{
    var newValue = Math.floor(Math.random()*9)+1;
    args = [].slice.call(arguments);
    var isNotNew = args.some( function( item ){
       item == newValue;
    });

    return !isNotNew ? newValue : en_Til_Ni.apply( this, args ) ;
}

现在此方法将始终返回唯一值

var a = en_Til_Ni();
console.log("a" + a);
var b = en_Til_Ni(a);
console.log("b" + b);
var c = en_Til_Ni(a,b);
console.log("c" + c);

答案 3 :(得分:0)

这是一个没有增量部分的递归。让我们看看ikkeLike()没有第一个条件:

function ikkeLike() {
  // We removed the first case for the simplicity
  if (a == b) {
    ikkeLike();
  }
}

现在你可以很容易地看到,在这种情况下它只是一个无限循环...... 您需要通过更改' a'来解决此问题。或者' b'在递归调用之前:

function ikkeLike() {      
  if (a == b) {
    // <--Change 'a' or 'b' here before the recursive call to avoid infinite loop!
    ikkeLike();
  }
}