在JavaScript中验证来自提示的用户输入

时间:2015-07-31 04:02:57

标签: javascript validation input

所以我从Codecademy扩展摇滚/纸/剪JS游戏以验证用户输入,但是当用户插入除&#之外的其他内容时,我无法让程序继续询问正确的用户输入39;摇滚''纸张'或者'剪刀'。

go run *.go

这在用户第一次输入“&#39; r&#39;提示,但如果第二次输入错误,它就不起作用,控制台在最后一行记录var userChoice; userChoice = prompt('Do you choose rock, paper, or scissors?'); console.log('User choice: ' + userChoice); if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') { userChoice = prompt('Select again.'); } else if (userChoice === computerChoice) { userChoice = prompt('It\'s a tie! Pick again.'); console.log('New user choice: ' + userChoice); } //computer random choice var computerChoice = Math.random(); console.log('Computer random number: ' + computerChoice); // assign rock, paper, scissors values if (computerChoice <= 0.33) { computerChoice = 'rock'; } else if (computerChoice <= 0.67) { computerChoice = 'paper'; } else { computerChoice = 'scissors'; } console.log('Computer choice: ' + computerChoice); // compare user and computer choices var compare = function(choice1, choice2) { if (choice1 === 'rock') { if (choice2 === 'scissors') { return 'Rock wins!'; } else { return 'Paper wins!'; } } else if (choice1 === 'scissors') { if (choice2 === 'rock') { return 'Rock wins!'; } else { return 'Scissors win!'; } } else if (choice1 === 'paper') { if (choice2 === 'rock') { return 'Paper wins!'; } else { return 'Scissors win!'; } } }; console.log(compare(userChoice, computerChoice)); undefined如何让它继续询问有效的输入?先谢谢你们!

3 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。要解决第一个问题,您可以这样做:

var userChoice;

userChoice = prompt('Do you choose rock, paper, or scissors?');
console.log('User choice: ' + userChoice);

while (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') {
    userChoice = prompt('Select again.');
} 

但是,第二部分仍有问题:

if (userChoice === computerChoice) {
    userChoice = prompt('It\'s a tie! Pick again.');
    console.log('New user choice: ' + userChoice);
}

此代码永远不会触发,因为您在进行比较后计算computerChoice。您应该将此代码移到compare函数中:

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return 'It\'s a tie!';
    } else if (choice1 === 'rock') {
        if (choice2 === 'scissors') {
            return 'Rock wins!';
        } else {
            return 'Paper wins!';
        }
    } else if (choice1 === 'scissors') {
        if (choice2 === 'rock') {
            return 'Rock wins!';
        } else {
            return 'Scissors win!';
        }
    } else if (choice1 === 'paper') {
        if (choice2 === 'rock') {
            return 'Paper wins!';
        } else {
            return 'Scissors win!';
        }
    }
};

答案 1 :(得分:0)

运行userChoice = prompt('Select again.');后,您只需继续完成剩余的代码执行。您需要的是某种循环条件,它检查它们是否已输入有效输入并让代码只有在有效时才继续。 (提示:&#34; while&#34;循环)

尝试以下内容:

//to do
// after it is a tie, making the same choice doesn't do anything?
// keep on prompting if incorrect input again

// take user input
var userChoice;

userChoice = prompt('Do you choose rock, paper, or scissors?');
console.log('User choice: ' + userChoice);

var valid = false;

//computer random choice
var computerChoice = Math.random();
console.log('Computer random number: ' + computerChoice);

// assign rock, paper, scissors values
if (computerChoice <= 0.33) {
    computerChoice = 'rock';
} else if (computerChoice <= 0.67) {
    computerChoice = 'paper';
} else {
    computerChoice = 'scissors';
}

while (!valid) {
    if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') {
        userChoice = prompt('Select again.');
    } else if (userChoice === computerChoice) {
        userChoice = prompt('It\'s a tie! Pick again.');
        //computer random choice
        var computerChoice = Math.random();
        console.log('Computer random number: ' + computerChoice);

        // assign rock, paper, scissors values
        if (computerChoice <= 0.33) {
            computerChoice = 'rock';
        } else if (computerChoice <= 0.67) {
            computerChoice = 'paper';
        } else {
            computerChoice = 'scissors';
        }
        console.log('New user choice: ' + userChoice);
    } else {
     valid = true;
    }
}


console.log('Computer choice: ' + computerChoice);

// compare user and computer choices
var compare = function(choice1, choice2) {
    if (choice1 === 'rock') {
        if (choice2 === 'scissors') {
            return 'Rock wins!';
        } else {
            return 'Paper wins!';
        }
    } else if (choice1 === 'scissors') {
        if (choice2 === 'rock') {
            return 'Rock wins!';
        } else {
            return 'Scissors win!';
        }
    } else if (choice1 === 'paper') {
        if (choice2 === 'rock') {
            return 'Paper wins!';
        } else {
            return 'Scissors win!';
        }
    }
};

console.log(compare(userChoice, computerChoice));

答案 2 :(得分:0)

  

它不起作用,控制台记录未定义

问题是,您缺少函数compare()中的var compare = function(choice1, choice2) { if { ... } return "oops !!"; //<-- missing }; 语句:

nopCommerce
  

如何让它继续询问有效输入?

使用循环可以在这里提供帮助。可能会......循环。