返回

javascript——我应该添加什么语法来启用这个循环?

发布时间:2022-04-09 05:33:17 492
# 前端

我正在尝试制作一个石头、布、剪刀游戏,作为我在JS中的第一个项目,但我很难让下面的代码循环并提示输入5次。此时,它只提示一次,然后接受该输入作为循环的相同输入5次。我已经在这方面坚持了几天,我找不到任何资源来教我。

注意:我必须使用提示框作为作业的一部分。

如果有人能修复这个代码,并告诉我哪里出了问题,这将是一个很大的帮助。

我曾尝试将玩家选择函数中的提示框添加到游戏函数循环中,但我无法定义它,也无法从游戏函数内部调用该函数,因此我不知所措。

//computer choice//
function computerPlay(){
        let numberGen = Math.floor(Math.random() * 3 + 1);
        if (numberGen === 1 ) {
            return  "rock"; }
        if (numberGen === 2) {
            return "paper"; }
         if (numberGen === 3) {
            return "scissors"; }
}
const computerOutput = computerPlay();

      //player choice//
function humanPlay(){ //this outputs our input//
     let popupmessage = prompt("Rock, Or Paper Or Scissors?")
      return popupmessage.trim().toLowerCase();
}
const playerInput = humanPlay()

//comparitor//
function playerSelection(playerInput, computerOutput) {
      if (playerInput === computerOutput) { 
            console.log("Computer picked the same"); }
      if (playerInput == "paper" && computerOutput == "scissors") {
            console.log("Computer picked scissors"); }
      if (playerInput == "paper" && computerOutput == "rock") {
            console.log("Computer picked rock"); }
      if (playerInput == "scissors" && computerOutput == "paper") {
            console.log("Computer picked paper"); }
      if (playerInput == "scissors" && computerOutput == "rock") {
            console.log("Computer picked rock"); }
      if (playerInput =="rock" && computerOutput == "scissors") {
            console.log("Computer picked scissors"); } 
      if (playerInput == "rock" && computerOutput == "paper") {
            console.log("Computer picked paper"); }
}
const comparitoroutput = playerSelection(humanPlay(), computerPlay());

// This is the bestof5 loop that computes a winner of the game // 
function game(comparitoroutput){
let computer = 0;
let player = 0;   


   for (let i = 0; i < 5; i++) { // this loops the comparatoroutput 5 times // 
      if (comparitoroutput === "computer wins"){
         player++;
         console.log("you lost!");
      } else {
         computer++; }
         console.log("you won!");
      if (player >= 3) {
         return "YOU WIN!"; }
      if (computer >= 3) {
         return "COMPUTER WINS!"; }

}
}
game()

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像