简体   繁体   English

从JavaScript中的提示验证用户输入

[英]Validate user input from prompt in JavaScript

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'. 因此,我从Codecademy扩展了rock / paper / scissors JS游戏以验证用户输入,但是当用户插入“ rock”,“ paper”以外的其他内容时,我无法让程序继续要求正确的用户输入。或“剪刀”。

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

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)); 第一次用户在提示符下输入类似'r'的命令时,此方法工作正常,但如果第二次输入错误,则该命令不起作用,并且控制台在最后一行console上undefined日志console.log(compare(userChoice, computerChoice)); How do I get it to keep on asking for the valid input? 我如何获得它以继续请求有效输入? Thanks in advance guys! 在此先感谢大家!

You have a few issues with your code. 您的代码有一些问题。 To solve the first problem you can do this: 要解决第一个问题,您可以这样做:

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.');
} 

however, you still have an issue with the second part: 但是,第二部分仍然存在问题:

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

This code will never fire because you calculate computerChoice AFTER making the comparison. 此代码将永远不会触发,因为您在进行比较后会计算出computerChoice You should move this code into your compare function: 您应该将此代码移到您的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!';
        }
    }
};

After you run userChoice = prompt('Select again.'); 运行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) (提示:“ while ”循环)

Try out the following: 尝试以下方法:

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

it doesn't work and the console logs undefined 它不起作用,控制台日志未定义

Problem is, you are missing the return statement in the function compare() : 问题是,您在功能compare()中缺少return语句:

var compare = function(choice1, choice2) {
    if {
      ...
    }
    return "oops !!"; //<-- missing
};

How do I get it to keep on asking for the valid input? 我如何获得它以继续请求有效输入?

Using loop can help here. 使用循环可以帮助您。 Possibly do..while loop. 可能做..while循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM