浏览器崩溃与简单的JS练习

时间:2016-08-25 09:01:29

标签: javascript arrays loops

我已经尝试在控制台中逐行调整此行,并且每行似乎都正常工作,但是当我输入连续的偶数时,我的浏览器会冻结。这可能是显而易见的,但它现在正在逃避我。代码如下:

$('body').append('<input type="number" id="myInput"><input type="submit" id="submit">');
$('#submit').click(function(){
        var myVal = $('#myInput').val();
        myArray = myVal.split("");
        for (var i = 0; i < myArray.length -1; i++){
            if((myArray[i]%2 == 0) && (myArray[i+1]%2 == 0)){
                myArray.splice(i,0,'-')
            }       
        }
        console.log(myArray);
});

1 个答案:

答案 0 :(得分:0)

我不确定您需要做什么,但此代码有效:

$('body').append('<input type="number" id="myInput"><input type="submit" id="submit">');
$('#submit').click(function(){
    var myVal = $('#myInput').val();
    myArray = myVal.split("");
    myArraylength = myArray.length - 1;
    for (var i = 0; i < myArraylength; i++){
        if((myArray[i]%2 == 0) && (myArray[i+1]%2 == 0)){
            myArray.splice(i+1,0,'-')
        }
    }
    console.log(myArray);
});