如何使我的函数 return 成为未定义以外的值?
How can I make my function return a value other than undefined?
我正在尝试完成奥丁计划中的石头剪刀布挑战。我卡住了;我不知道为什么当我 运行 我的代码时,我的函数 playRound returns 未定义。任何帮助将不胜感激。
const computerPlay = () => { //Randomly returns 'Rock', 'Paper', or 'Scissors'
let choices = ['rock', 'paper', 'scissors'];
let computerChoice = choices[Math.floor(Math.random() * choices.length)];
return computerChoice;
}
const playRound = (playerSelection, computerSelection) => {
playerSelection = playerSelection.toLowerCase;
if (playerSelection === computerSelection) {
return "It's a tie";
} else {
switch (playerSelection.toLowerCase) {
case 'rock':
switch (computerSelection) {
case 'paper':
return "You Lose! Paper beats Rock";
case 'scissors':
return "You Win! Rock beats Scissors";
}
case 'paper':
switch (computerSelection) {
case 'rock':
return "You Win! Paper beats Rock";
case 'scissors':
return "You Lose. Scissors beats Paper"
}
case 'scissors':
switch(computerSelection) {
case 'rock':
return "You Lose. Rock beats scissors";
case 'paper':
return "You Win. Scissors beats Paper"
}
}
}
}
const playerSelection = "rock";
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
您在 toLowerCase 方法中缺少括号。
const computerPlay = () => { //Randomly returns 'Rock', 'Paper', or 'Scissors'
let choices = ['rock', 'paper', 'scissors'];
let computerChoice = choices[Math.floor(Math.random() * choices.length)];
return computerChoice;
}
const playRound = (playerSelection, computerSelection) => {
playerSelection = playerSelection.toLowerCase();
if (playerSelection === computerSelection) {
return "It's a tie";
} else {
switch (playerSelection.toLowerCase()) {
case 'rock':
switch (computerSelection) {
case 'paper':
return "You Lose! Paper beats Rock";
case 'scissors':
return "You Win! Rock beats Scissors";
}
case 'paper':
switch (computerSelection) {
case 'rock':
return "You Win! Paper beats Rock";
case 'scissors':
return "You Lose. Scissors beats Paper"
}
case 'scissors':
switch(computerSelection) {
case 'rock':
return "You Lose. Rock beats scissors";
case 'paper':
return "You Win. Scissors beats Paper"
}
}
}
}
const playerSelection = "rock";
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
您缺少括号 -> .toLowerCase()
const sentence = 'My Dog Is Brown';
console.log(sentence.toLowerCase());
// expected output: "my dog is brown"
.toLowerCase()方法returns将字符串的值转换为小写
.toLowerCase() 不影响字符串本身的值。
我正在尝试完成奥丁计划中的石头剪刀布挑战。我卡住了;我不知道为什么当我 运行 我的代码时,我的函数 playRound returns 未定义。任何帮助将不胜感激。
const computerPlay = () => { //Randomly returns 'Rock', 'Paper', or 'Scissors'
let choices = ['rock', 'paper', 'scissors'];
let computerChoice = choices[Math.floor(Math.random() * choices.length)];
return computerChoice;
}
const playRound = (playerSelection, computerSelection) => {
playerSelection = playerSelection.toLowerCase;
if (playerSelection === computerSelection) {
return "It's a tie";
} else {
switch (playerSelection.toLowerCase) {
case 'rock':
switch (computerSelection) {
case 'paper':
return "You Lose! Paper beats Rock";
case 'scissors':
return "You Win! Rock beats Scissors";
}
case 'paper':
switch (computerSelection) {
case 'rock':
return "You Win! Paper beats Rock";
case 'scissors':
return "You Lose. Scissors beats Paper"
}
case 'scissors':
switch(computerSelection) {
case 'rock':
return "You Lose. Rock beats scissors";
case 'paper':
return "You Win. Scissors beats Paper"
}
}
}
}
const playerSelection = "rock";
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
您在 toLowerCase 方法中缺少括号。
const computerPlay = () => { //Randomly returns 'Rock', 'Paper', or 'Scissors'
let choices = ['rock', 'paper', 'scissors'];
let computerChoice = choices[Math.floor(Math.random() * choices.length)];
return computerChoice;
}
const playRound = (playerSelection, computerSelection) => {
playerSelection = playerSelection.toLowerCase();
if (playerSelection === computerSelection) {
return "It's a tie";
} else {
switch (playerSelection.toLowerCase()) {
case 'rock':
switch (computerSelection) {
case 'paper':
return "You Lose! Paper beats Rock";
case 'scissors':
return "You Win! Rock beats Scissors";
}
case 'paper':
switch (computerSelection) {
case 'rock':
return "You Win! Paper beats Rock";
case 'scissors':
return "You Lose. Scissors beats Paper"
}
case 'scissors':
switch(computerSelection) {
case 'rock':
return "You Lose. Rock beats scissors";
case 'paper':
return "You Win. Scissors beats Paper"
}
}
}
}
const playerSelection = "rock";
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
您缺少括号 -> .toLowerCase()
const sentence = 'My Dog Is Brown';
console.log(sentence.toLowerCase());
// expected output: "my dog is brown"
.toLowerCase()方法returns将字符串的值转换为小写
.toLowerCase() 不影响字符串本身的值。