JavaScript我需要帮助验证并显示错误消息

时间:2014-03-07 01:43:43

标签: javascript

function Pin(valid) {

    var Num;
    var Attempts = 0;

    while (Attempts < 3) {
        Attempts = (Attempts + 1);
        Num = prompt('Enter the pin number');

        if (Num != '12345') {
            alert('Wrong pin number, this is attemp ' + Attempts + ' of 3');
        } else {
            alert('Welcome!');
            valid = true;
            return valid;
            return;
        }
    }
}

我需要帮助向我展示如何在3次尝试失败后显示错误消息并结束程序。我尝试了很多方法,但似乎无法弄明白。

1 个答案:

答案 0 :(得分:1)

如果循环结束,则表示用户超过了最大尝试次数。所以在那里显示错误信息。

function Pin(valid) {

    var Num;
    var Attempts = 0;

    while (Attempts < 3) {
        Attempts++;
        Num = prompt('Enter the pin number');

        if (Num != '12345') {
            alert('Wrong pin number, this is attemp ' + Attempts + ' of 3');
        } else {
            alert('Welcome!');
            return true;
        }
    }
    alert('Too many failed attempts, giving up');
    return false;
}