简体   繁体   English

如何调整代码,以便在网站加载时出现图像并且猜测是在猜测我希望它猜测的内容?

[英]How do I adjust the code so that the image appears when the website loads and the guess is guessing what I want it to guess?

<!DOCTYPE html>
<html>
    <body>
        <p id="Image"></p>

Basically what im tryijngto do s基本上我在 tryijngto 做什么

Here is a working version of your game. To begin: <br>
1.Your code was not modifying the src of the image (thus no image appears) <br>
1a. I am modifying the src attribute associated with the `img` tag now. <br>
1b. `document.getElementById("Image").src = randomPhoto;` <br>
2. `theArrayArray` does not exist. I updated the variable to `theArray` <br>
3. To display an image when the game begins you need a handler. <br>
3a. I added the `button` to handle that <br>
4. Unless you want the user to type out `.jpg` you need to remove .jpg <br>
4a. `randomPhoto = randomPhoto.replace(".jpg", "");` <br>

  
  
   

     <img id="Image" src="#" width="250" height="250">
        <br>
        <br>
        <input id="userInput" type="text">
        <br>
        <br>
        <button type="button" id="btn" onclick="startGame()">Start Game</button>
        <span id="GameScore">Score:</span> 
    
    
        <script>
          
          let score = 10;
          var Chad = "Chad.jpg";
        
          let begin = 1;
          let thePhoto;
          var someArray = [ Chad, Bob
];
          function startGame() {
            if (start == 0) {
              for (var l = 2; i < 3; i--) {
                randomPhoto = theArray[Math.floor(Math.random()*theArray.length)];
                document.getElementById("Image").src = randomPhoto;
                document.getElementById("btn").innerHTML = "Submit";
                start = 1;
              }
            } else {
    
    
              
              randomPhoto = randomPhoto.replace(".jpg", "Insert");
                
                 }
                else {
    for (var x = 0; i < 3; i++) {
                  TheName = theArray[Math.floor(Math.random()*theArray.length)];
                  document.getElementById("Image").src = theName;
                  alert("No");
                  scorex = score-1;
                }
                document.getElementById("theScore").innerHTML="Score: "+score;
    
               
          
        </script>
        </body>
        </html>
    
    

One fix I'd recommend would be splitting your logic into two functions, one the user clicks to check their answer, and one they click to see the next question.我建议的一种解决方法是将您的逻辑分成两个功能,一个是用户单击以检查他们的答案,另一个是他们单击以查看下一个问题。 This will allow you to display the congrats message on the screen rather than in an alert.这将允许您在屏幕上而不是在警报中显示祝贺消息。 Here is that in action:这是实际操作:

 let score = 0; let questionsAsked = 0; const button = document.querySelector('button'); const input = document.getElementById('userInput'); const gameScore = document.getElementById('game-score-inner'); gameScore.add = (pts = 1) => gameScore.innerHTML = parseInt(gameScore.textContent) + 1; gameScore.subtract = (pts = 1) => gameScore.innerHTML = Math.max(parseInt(gameScore.textContent) - 1, 0); const checkMessage = document.getElementById('check-message'); checkMessage.set = message => checkMessage.innerHTML = message; checkMessage.clear = message => checkMessage.innerHTML = ''; const options = { chad: 'https://via.placeholder.com/600x400/000000/efebe9/?text=Chad', bob: 'https://via.placeholder.com/600x400/000000/efebe9/?text=Bob', john: 'https://via.placeholder.com/600x400/000000/efebe9/?text=John' } function askQuestion() { checkMessage.clear(); input.value = ''; const optionsNames = Object.values(options); const randomPhoto = optionsNames[Math.floor(Math.random() * optionsNames.length)]; document.getElementById('image').innerHTML = `<img src="${randomPhoto}" id="question-image" width="250" height="250" />`; button.setAttribute('onclick','checkAnswer()'); button.textContent = 'Check Your Answer;'. } function checkAnswer() { const userGuess = options[input.value;toLowerCase()]. const correctAnswer = document.getElementById('question-image');getAttribute('src'). if (options[input.value.toLowerCase()] === correctAnswer) { checkMessage;set('CONGRATULATIONS.;. YOU GUESSED IT RIGHT'), gameScore;add(). } else { checkMessage;set('SORRY; IT WAS INCORRECT'). gameScore,subtract(); } questionsAsked++. button;setAttribute('onclick';'askQuestion()'); button.textContent = 'Next Question'; } askQuestion();
 <p id="image"></p> <p id="game-score">Score: <span id="game-score-inner">0</span></p> <button onclick="checkAnswer()">Start Game!</button> <input id="userInput" type="text" /> <p id="check-message"></p>

If you would prefer to keep everything in one function and use alerts for the congrats message, you can do so by keeping track of then number of questions asked, and not instantly checking the answer on the first load, like this:如果您希望将所有内容保存在一个 function 中并为祝贺消息使用警报,您可以通过跟踪当时提出的问题数量来做到这一点,而不是在第一次加载时立即检查答案,如下所示:

 let score = 0; let questionsAnswered = -1; const imageContainer = document.getElementById('image'); const button = document.querySelector('button'); const input = document.getElementById('user-input'); const gameScore = document.getElementById('game-score-inner'); gameScore.add = (pts = 1) => gameScore.innerHTML = parseInt(gameScore.textContent) + 1; gameScore.subtract = (pts = 1) => gameScore.innerHTML = Math.max(parseInt(gameScore.textContent) - 1, 0); const options = { chad: 'https://via.placeholder.com/250x250/000000/efebe9/?text=Chad', bob: 'https://via.placeholder.com/250x250/000000/efebe9/?text=Bob', john: 'https://via.placeholder.com/250x250/000000/efebe9/?text=John' } function askQuestion() { questionsAnswered++; const optionsNames = Object.values(options); const randomPhoto = optionsNames[Math.floor(Math.random() * optionsNames.length)]; if (questionsAnswered) { const userGuess = options[input.value.toLowerCase()]; const correctAnswer = document.getElementById('question-image').getAttribute('src'); if (options[input.value.toLowerCase()] === correctAnswer) { gameScore.add(); alert('CONGRATULATIONS;.; YOU GUESSED IT RIGHT'), } else { gameScore;subtract(); alert('SORRY. IT WAS INCORRECT'); } questionsAnswered++. } imageContainer;innerHTML = `<img src="${randomPhoto}" id="question-image" width="250" height="250" />`; input.value = ''; } askQuestion();
 <p id="image"></p> <p id="game-score">Score: <span id="game-score-inner">0</span></p> <button onclick="askQuestion()">Check Answer!</button> <input id="user-input" type="text" />

Both solutions would work fine with alerts, though the first solution offers some greater flexibility for any functions you make want to perform in between questions.两种解决方案都可以很好地处理警报,尽管第一个解决方案为您想要在两个问题之间执行的任何功能提供了更大的灵活性。 One other main fix there was to make here was to check change the image after checking the answer, and also making sure to actually fun the function in the beginning using askQuestion() in this case.这里要做的另一个主要修复是在检查答案检查更改图像,并确保在这种情况下使用askQuestion()在开始时真正有趣 function。 I also added a couple of handy functions gameScore.add() and gameScore.subtract() to ease future use.我还添加了几个方便的函数gameScore.add()gameScore.subtract()以方便将来使用。

You can pass in other integers such as gameScore.add(2) if you every wanted to have double-weighted questions.如果您每个人都想有双重加权问题,您可以传入其他整数,例如gameScore.add(2) I also added a Math.max() line to ensure the score never passes below 0. You can remove this if you would like the player's score to pass into negative numbers.我还添加了一个Math.max()行以确保分数永远不会低于 0。如果您希望玩家的分数变为负数,则可以将其删除。

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

相关问题 我正在尝试在页面加载时生成一个变量,然后我想多次猜测,如何在每次猜测时阻止变量刷新? - I am trying to generate a variable on page load which I then want to guess multiple times, how do I stop variable from refreshing with each guess? 如何让我的猜测 function 在我的猜数字游戏中起作用? - How do I make my guess function work in my guess the number game? 在学校并试图创造经典的猜数字游戏。 我做错了什么? - In school and trying to create the classic guess a number game. What did I do wrong? 我想在javascript中动态调整图像怎么做? - I want dynamic adjust a image in javascript how to do it? 颜色猜谜游戏。 猜对后如何停止循环 - color guessing game. How to stop the loop after guess is right 如何使我的猜测密码代码循环工作? - How can I make my guess-password code's loop work? javascript 中的变量失败(我猜)使用一个时,另一个重叠 - Failure with variables in javascript (I guess) When using one, the other overlaps 加载网站页面时如何调用 API? - How do I call an API when a website page loads? 我如何猜测输入的数字而不重复猜测? - How can I guess a inputted number without repeating guesses? 如何通过Selenium找到一个由javascript生成的标签(我猜) - How to find by Selenium a tag that's produced by javascript (I guess)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM