My input always returns 0 when using the .target.innerHTML.

huangapple go评论76阅读模式
英文:

My input always returns 0 when using the .target.innerHTML

问题

I am currently creating a UI for my Rock, Paper, Scissors game program in JavaScript. While changing pieces of my code, I found out that my variable playerChoice's value will always end up as 0 when running the function playRound(playerChoice, getComputerChoice()). However, when inputting similar plain strings such as ROCK, PAPER, or SCISSORS, the code/game runs fine.

I tried following the path where it's being passed unto so I used console.log:

  • Under Line 51 as console.log(typeof playerChoice) it's string
  • Under Line 26 as console.log(typeof playerChecker) it's string

So far so good because it remains as the same data type. Now It's entering my function inputConverter() because of the variable let playerChoice. But when I use console.log:

  • Under Line 29 as console.log(playerChoice), it WILL ALWAYS end up as 0. But the variable chosen by the computer doesn't. Why is this?

I've tried using debugging tools and all, but in the end it doesn't show up because it's a logical error. I thought it would be because I used e.target.innerHTML, but I'm unsure if that's truly the case.

function getComputerChoice() {
  // 2 is for Scissors, 1 is for Rock, 0 is for Paper
  const compChoice = Math.floor(Math.random() * 3);
  if (compChoice === 2) {
    return "Scissors";
  } else if (compChoice === 1) {
    return "Rock";
  } else
    return "Paper";
}

function inputConverter(gamerInput) {
  if (gamerInput === "ROCK") {
    return 2;
  } else if (gamerInput === "PAPER") {
    return 1;
  } else
    return 0;
}

function playRound(playerSelection, computerSelection) {
  let playerChecker = playerSelection.toUpperCase();
  let computerChecker = computerSelection.toUpperCase();

  let playerChoice = inputConverter(playerChecker);
  let computerChoice = inputConverter(computerChecker);

  if (playerChoice === computerChoice) {
    return "Draw! " + playerChecker + " is the same as " + computerChecker;
  } else if (playerChoice === 2 && computerChoice === 0 ||
    playerChoice === 1 && computerChoice === 2 ||
    playerChoice === 0 && computerChoice === 1) {
    return "You Win! " + playerChecker + " beats " + computerChecker;
  } else {
    return "You Lose! " + computerChecker + " beats " + playerChecker;
  }
}

let buttonChoices = document.querySelectorAll("button");

buttonChoices.forEach(i => {
  i.addEventListener("click", (e) => {
    let playerChoice = e.target.innerHTML;
    console.log(playRound(playerChoice, getComputerChoice()));
  })
})
<div id="choiceContainer">
  <button id="btn"> Rock </button>
  <button id="btn"> Paper </button>
  <button id="btn"> Scissors </button>
</div>
英文:

I am currently creating a UI for my Rock, Paper, Scissors game program in JavaScript. While changing pieces of my code, I found out that my variable playerChoice's value will always end up as 0 when running the function playRound(playerChoice, getComputerChoice()). However, when inputting similar plain strings such as ROCK, PAPER, or SCISSORS, the code/game runs fine.

I tried following the path where it's being passed unto so I used console.log:

  • Under Line 51 as console.log(typeof playerChoice) it's string
  • Under Line 26 as console.log(typeof playerChecker) it's string

So far so good because it remains as the same data type. Now It's entering my function inputConverter() because of the variable let playerChoice. But when I use console.log:

  • Under Line 29 as console.log(playerChoice), it WILL ALWAYS end up as 0. But the variable chosen by the computer doesn't. Why is this?

I've tried using debugging tools and all, but in the end it doesn't show up because it's a logical error. I thought it would be because I used e.target.innerHTML, but I'm unsure if that's truly the case.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function getComputerChoice() {
// 2 is for Scissors, 1 is for Rock, 0 is for Paper
const compChoice = Math.floor(Math.random() * 3);
if (compChoice === 2) {
return &quot;Scissors&quot;;
} else if (compChoice === 1) {
return &quot;Rock&quot;;
} else
return &quot;Paper&quot;;
}
function inputConverter(gamerInput) {
if (gamerInput === &quot;ROCK&quot;) {
return 2;
} else if (gamerInput === &quot;PAPER&quot;) {
return 1;
} else
return 0;
}
function playRound(playerSelection, computerSelection) {
let playerChecker = playerSelection.toUpperCase();
let computerChecker = computerSelection.toUpperCase();
let playerChoice = inputConverter(playerChecker);
let computerChoice = inputConverter(computerChecker);
if (playerChoice === computerChoice) {
return &quot;Draw! &quot; + playerChecker + &quot; is the same as &quot; + computerChecker;
} else if (playerChoice === 2 &amp;&amp; computerChoice === 0 ||
playerChoice === 1 &amp;&amp; computerChoice === 2 ||
playerChoice === 0 &amp;&amp; computerChoice === 1) {
return &quot;You Win! &quot; + playerChecker + &quot; beats &quot; + computerChecker;
} else {
return &quot;You Lose! &quot; + computerChecker + &quot; beats &quot; + playerChecker;
}
}
let buttonChoices = document.querySelectorAll(&quot;button&quot;);
buttonChoices.forEach(i =&gt; {
i.addEventListener(&quot;click&quot;, (e) =&gt; {
let playerChoice = e.target.innerHTML;
console.log(playRound(playerChoice, getComputerChoice()));
})
})

<!-- language: lang-html -->

&lt;div id=&quot;choiceContainer&quot;&gt;
&lt;button id=&quot;btn&quot;&gt; Rock &lt;/button&gt;
&lt;button id=&quot;btn&quot;&gt; Paper &lt;/button&gt;
&lt;button id=&quot;btn&quot;&gt; Scissors &lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 4

首先,id 必须是唯一的。

其次,您尝试将带有空格的字符串与不带空格的字符串进行比较,最好使用 textContenttrim,如下所示:

function getComputerChoice() {
  // 2 表示剪刀,1 表示石头,0 表示纸
  const compChoice = Math.floor(Math.random() * 3);
  if (compChoice === 2) {
    return "剪刀";
  } else if (compChoice === 1) {
    return "石头";
  } else
    return "纸";
}

function inputConverter(gamerInput) {
  if (gamerInput === "石头") {
    return 2;
  } else if (gamerInput === "纸") {
    return 1;
  } else
    return 0;
}

function playRound(playerSelection, computerSelection) {
  let playerChecker = playerSelection.toUpperCase();
  let computerChecker = computerSelection.toUpperCase();

  let playerChoice = inputConverter(playerChecker);
  let computerChoice = inputConverter(computerChecker);

  if (playerChoice === computerChoice) {
    return "平局! " + playerChecker + " 与 " + computerChecker + " 相同";
  } else if (playerChoice === 2 && computerChoice === 0 ||
    playerChoice === 1 && computerChoice === 2 ||
    playerChoice === 0 && computerChoice === 1) {
    return "你赢了! " + playerChecker + " 战胜了 " + computerChecker;
  } else {
    return "你输了! " + computerChecker + " 战胜了 " + playerChecker;
  }
}

let buttonChoices = document.querySelectorAll("button");

buttonChoices.forEach(i => {
  i.addEventListener("click", (e) => {
    let playerChoice = e.target.textContent.trim();    
    console.log(playRound(playerChoice, getComputerChoice()));
  })
})

HTML 部分:

<div id="choiceContainer">
  <button class="btn">石头</button>
  <button class="btn">纸</button>
  <button class="btn">剪刀</button>
</div>

参考链接:

英文:

First of all id must be unique.
Second you try to compare string with space with string without space, it's better use textContent with trim like:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function getComputerChoice() {
// 2 is for Scissors, 1 is for Rock, 0 is for Paper
const compChoice = Math.floor(Math.random() * 3);
if (compChoice === 2) {
return &quot;Scissors&quot;;
} else if (compChoice === 1) {
return &quot;Rock&quot;;
} else
return &quot;Paper&quot;;
}
function inputConverter(gamerInput) {
if (gamerInput === &quot;ROCK&quot;) {
return 2;
} else if (gamerInput === &quot;PAPER&quot;) {
return 1;
} else
return 0;
}
function playRound(playerSelection, computerSelection) {
let playerChecker = playerSelection.toUpperCase();
let computerChecker = computerSelection.toUpperCase();
let playerChoice = inputConverter(playerChecker);
let computerChoice = inputConverter(computerChecker);
if (playerChoice === computerChoice) {
return &quot;Draw! &quot; + playerChecker + &quot; is the same as &quot; + computerChecker;
} else if (playerChoice === 2 &amp;&amp; computerChoice === 0 ||
playerChoice === 1 &amp;&amp; computerChoice === 2 ||
playerChoice === 0 &amp;&amp; computerChoice === 1) {
return &quot;You Win! &quot; + playerChecker + &quot; beats &quot; + computerChecker;
} else {
return &quot;You Lose! &quot; + computerChecker + &quot; beats &quot; + playerChecker;
}
}
let buttonChoices = document.querySelectorAll(&quot;button&quot;);
buttonChoices.forEach(i =&gt; {
i.addEventListener(&quot;click&quot;, (e) =&gt; {
let playerChoice = e.target.textContent.trim();    
console.log(playRound(playerChoice, getComputerChoice()));
})
})

<!-- language: lang-html -->

&lt;div id=&quot;choiceContainer&quot;&gt;
&lt;button class=&quot;btn&quot;&gt; Rock &lt;/button&gt;
&lt;button class=&quot;btn&quot;&gt; Paper &lt;/button&gt;
&lt;button class=&quot;btn&quot;&gt; Scissors &lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

Reference:

答案2

得分: 0

以下是代码的翻译部分:

let options = ["rock", "paper", "scissors"];
let playerChoice = null;
let computerChoice = null;
let rock = null;
let paper = null;
let scissors = null;
let results = null;
let choices = null;
let score = null;
let playerScore = 0;
let computerScore = 0;

document.addEventListener("DOMContentLoaded", addListeners);

function addListeners() {
    rock = document.getElementById("rock");
    paper = document.getElementById("paper");
    scissors = document.getElementById("scissors");
    choices = document.getElementById("choicesDiv");
    results = document.getElementById("displayResults");
    score = document.getElementById("scoreDsp");
    rock.addEventListener("click", () => { play(0); });
    paper.addEventListener("click", () => { play(1); });
    scissors.addEventListener("click", () => { play(2); });
}

function play(element) {
    playerChoice = options[element];
    computerChoice = computerPlay();
    choices.innerHTML = ("玩家选择 = " + playerChoice + " 电脑选择 = " + computerChoice);
    computeScore();
}

function computerPlay() {
    var rnd = Math.floor(Math.random() * options.length);
    var result = options[rnd];
    return result;
}

function computeScore() {
    if (playerChoice == computerChoice) {
        results.innerHTML = "平局";
    } else if (
        (playerChoice == "rock" && computerChoice == "scissors") ||
        (playerChoice == "paper" && computerChoice == "rock") ||
        (playerChoice == "scissors" && computerChoice == "paper")
    ) {
        results.innerHTML = playerChoice + " 赢了 " + computerChoice + "。你赢了!";
        playerScore += 1;
    } else {
        results.innerHTML = computerChoice + " 赢了 " + playerChoice + "。电脑赢了!";
        computerScore += 1;
    }

    score.innerHTML = "玩家得分 = " + playerScore + ",电脑得分 = " + computerScore;
}

这是JavaScript部分的翻译。请注意,HTML部分没有进行翻译,因为它包含了一些HTML标记,而HTML标记通常不需要翻译。

英文:

An easier answer than this one I posted here :

https://stackoverflow.com/questions/75624474/how-to-make-the-computer-pick-another-random-selection-after-the-player-chooses/75626686#75626686

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

		let options = [&quot;rock&quot;, &quot;paper&quot;, &quot;scissors&quot;];
let playerChoice = null;
let computerChoice = null;
let rock = null;
let paper = null;
let scissors = null;
let results = null;
let choices = null;
let score = null;
let playerScore = 0;
let computerScore = 0;
document.addEventListener(&quot;DOMContentLoaded&quot;,addListeners);
function addListeners(){
rock = document.getElementById(&quot;rock&quot;);
paper = document.getElementById(&quot;paper&quot;);
scissors = document.getElementById(&quot;scissors&quot;);
choices = document.getElementById(&quot;choicesDiv&quot;);
results = document.getElementById(&quot;displayResults&quot;);
score = document.getElementById(&quot;scoreDsp&quot;);
rock.addEventListener(&quot;click&quot;,() =&gt; {play(0);});
paper.addEventListener(&quot;click&quot;,() =&gt; {play(1);});
scissors.addEventListener(&quot;click&quot;,() =&gt; {play(2);});
}
function play(element){
playerChoice = options[element];
computerChoice = computerPlay();
choices.innerHTML = (&quot;player = &quot; + playerChoice + &quot; computer = &quot; + computerChoice);
computeScore();
}
function computerPlay(){
var rnd = Math.floor(Math.random()*options.length);
var result = options[rnd];
return result;
}
function computeScore(){
if (playerChoice == computerChoice) {
results.innerHTML = &quot;Its a tie&quot;;
}else if(
(playerChoice == &quot;rock&quot; &amp;&amp; computerChoice == &quot;scissors&quot;) ||
(playerChoice == &quot;paper&quot; &amp;&amp; computerChoice == &quot;rock&quot;) ||
(playerChoice == &quot;scissors&quot; &amp;&amp; computerChoice == &quot;paper&quot;)
){
results.innerHTML = playerChoice + &quot; beats &quot; + computerChoice + &quot;. YOU WIN!&quot;;
playerScore+=1;
}else{
results.innerHTML = computerChoice + &quot; beats &quot; + playerChoice + &quot;. COMPUTER WIN!&quot;;
computerScore+=1;
}
score.innerHTML = &quot;player score = &quot; + playerScore + &quot;, computer score = &quot; + computerScore;
}

<!-- language: lang-html -->

	&lt;div id=&quot;choiceContainer&quot;&gt;
&lt;button id=&quot;rock&quot; class=&quot;btn&quot;&gt; Rock &lt;/button&gt;
&lt;button id=&quot;paper&quot; class=&quot;btn&quot;&gt; Paper &lt;/button&gt;
&lt;button id=&quot;scissors&quot; class=&quot;btn&quot;&gt; Scissors &lt;/button&gt;
&lt;/div&gt;
&lt;div id=&quot;choicesDiv&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;displayResults&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;scoreDsp&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月31日 15:49:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定