功能中的代码在运行时不会运行。如果写在外面的功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了功能中的代码在运行时不会运行。如果写在外面的功能相关的知识,希望对你有一定的参考价值。
我目前的作业有些问题。
内部代码(while循环)函数startGame()在函数内部写入时不会运行。如果我删除函数声明它按预期工作。我究竟做错了什么?
let playerScore = 0;
let computerScore = 0;
let playerSelection;
let computerSelection;
let i = 0;
function startGame() {
while (i < 5) { // This part wont run when used inside function (){}
playRound();
i += 1;
}
}
/*
for (i = 0; i < 6; i++) {
playRound();
}
*/
/* NOTE TO SELF:
Can't do call to function before declaring variables.
Since they then would be undefined during runtime. */
function playRound() {
playerSelection = prompt();
computerSelection = computerPlay();
if (playerSelection.trim() === computerSelection.trim()) {
console.log("Round is Equal")
} else if (playerSelection === "Scissor" && computerSelection === "Paper") {
playerScore += 1;
console.log("Player score is:" + " " + playerScore + " " + "Player wins round");
} else if (playerSelection === "Paper" && computerSelection === "Scissor") {
computerScore += 1;
console.log("Computer score is:" + " " + computerScore + " " + "Computer win round");
} else if (playerSelection === "Rock" && computerSelection === "Scissor") {
playerScore += 1;
console.log("Player score is:" + " " + playerScore + " " + "Player wins round");
} else if (playerSelection === "Scissor" && computerSelection === "Rock") {
computerScore += 1;
console.log("Computer score is:" + " " + computerScore + " " + "Computer win round");
} else if (playerSelection === "Paper" && computerSelection === "Rock") {
playerScore += 1;
console.log("Player score is:" + " " + playerScore + " " + "Player wins round");
} else if (playerSelection === "Rock" && computerSelection === "Paper") {
computerScore += 1;
console.log("Computer score is:" + " " + computerScore + " " + "Computer win round");
}
}
function computerPlay() {
let randomNumber = Math.floor(Math.random() * 3) + 1;
switch (randomNumber) {
case 1:
randomNumber = "Rock";
return randomNumber;
case 2:
randomNumber = "Scissor";
return randomNumber;
case 3:
randomNumber = "Paper"
return randomNumber;
}
}
答案
你必须调用startGame
函数:
startGame()
另一答案
你需要对startGame()
函数进行初始调用,而不是自己运行。
let playerScore = 0;
let computerScore = 0;
let playerSelection;
let computerSelection;
let i = 0;
function startGame() {
while (i < 5) { // This part wont run when used inside function (){}
playRound();
i += 1;
}
}
startGame();
/*
for (i = 0; i < 6; i++) {
playRound();
}
*/
/* NOTE TO SELF:
Can't do call to function before declaring variables.
Since they then would be undefined during runtime. */
function playRound() {
playerSelection = prompt();
computerSelection = computerPlay();
if (playerSelection.trim() === computerSelection.trim()) {
console.log("Round is Equal")
} else if (playerSelection === "Scissor" && computerSelection === "Paper") {
playerScore += 1;
console.log("Player score is:" + " " + playerScore + " " + "Player wins round");
} else if (playerSelection === "Paper" && computerSelection === "Scissor") {
computerScore += 1;
console.log("Computer score is:" + " " + computerScore + " " + "Computer win round");
} else if (playerSelection === "Rock" && computerSelection === "Scissor") {
playerScore += 1;
console.log("Player score is:" + " " + playerScore + " " + "Player wins round");
} else if (playerSelection === "Scissor" && computerSelection === "Rock") {
computerScore += 1;
console.log("Computer score is:" + " " + computerScore + " " + "Computer win round");
} else if (playerSelection === "Paper" && computerSelection === "Rock") {
playerScore += 1;
console.log("Player score is:" + " " + playerScore + " " + "Player wins round");
} else if (playerSelection === "Rock" && computerSelection === "Paper") {
computerScore += 1;
console.log("Computer score is:" + " " + computerScore + " " + "Computer win round");
}
}
function computerPlay() {
let randomNumber = Math.floor(Math.random() * 3) + 1;
switch (randomNumber) {
case 1:
randomNumber = "Rock";
return randomNumber;
case 2:
randomNumber = "Scissor";
return randomNumber;
case 3:
randomNumber = "Paper"
return randomNumber;
}
}
另一答案
你已经创建了一个名为startGame()的函数,但是你没有在任何地方调用它,这就是它没有在函数中运行的原因。
就像你在循环中调用playRound()的方式一样,你也必须调用startGame()来运行它中的代码。
以上是关于功能中的代码在运行时不会运行。如果写在外面的功能的主要内容,如果未能解决你的问题,请参考以下文章
即使运行代码,resignFirstResponder 也不会关闭键盘
Postgres 查询不会在功能中完成,但如果单独运行它可以工作
replace File.separator出现异常:java.lang.IllegalArgumentException: character to be escaped is missing((代
replace File.separator出现异常:java.lang.IllegalArgumentException: character to be escaped is missing((代