验证JavaScript中提示中的用户输入 [英] Validate user input from prompt in JavaScript

查看:115
本文介绍了验证JavaScript中提示中的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我从Codecademy扩展了Rock/paper/Scissors JS游戏,以验证用户输入,但是当用户插入"rock"以外的内容时,我无法让程序继续要求正确的用户输入, 纸"或剪刀".

So I was extending the rock/paper/scissors JS game from Codecademy to validate user input, but I can't get the program to keep on asking for the right user input when the user inserts something other than 'rock', 'paper' or 'scissors'.

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));

在用户第一次在提示符下输入"r"之类的命令时,此方法工作正常,但如果第二次输入错误,则该命令将无效,并且控制台在最后一行undefined >如何获取要求继续输入的有效信息?预先感谢大家!

This works fine the first time the user enters something like 'r' to the prompt, but if the input is something wrong the second time, it doesn't work and the console logs undefined on the last line console.log(compare(userChoice, computerChoice)); How do I get it to keep on asking for the valid input? Thanks in advance guys!

推荐答案

运行userChoice = prompt('Select again.');之后,您只需继续完成其余的代码执行即可.您需要某种循环条件,检查它们是否输入了有效输入,并让代码仅在有效后才继续. (提示:" while 循环)

After you run userChoice = prompt('Select again.');, you just continue on to complete the rest of the code execution. What you need is some kind of looping condition that checks if they have entered valid input and lets the code continue only once it is valid. (hint: "while" loops)

尝试以下方法:

//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));

这篇关于验证JavaScript中提示中的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆