最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to display console.log output from JavaScript to HMTL5 page - Stack Overflow

programmeradmin5浏览0评论

The goal is run the game.html, so that it will call the JavaScript and run rockpapergame.js. Using the Chrome Browser, then Inspect, at Sources, I can look at the debugger and step over the function for the referenced js at script src="rockpapergame.js", and step through the rockpapergame.js. However, I get only the prompt for: userChoice = prompt("Do you choose rock, paper or scissors?").

How do I return the console.log output to the HTML5 page?

// Rock, Paper, Scissors Game //
// rockpapergame.js          //

var userChoice = prompt("Do you choose rock, paper or scissors?");
var puterChoice = Math.random();
if(puterChoice < 0.34) {
    puterChoice = "rock";
}else if (puterChoice <= 0.67) {
    puterChoice = "paper";
}else{
    puterChoice = "scissors";
}
console.log("Computer: " + puterChoice);

var pare = function(choice1, choice2)
{
    if (choice1 === choice2) {
      return "The result is a tie!";
    }else if (choice1 === "rock") {
        if (choice2 === "scissors") {
          return "rock wins";
        }else if (choice1 === "paper") {
            if (choice2 === "rock") {
              return "paper wins";
            }else {
              return "scissors wins";
            }
        }else {
          return "paper wins";
        }
    }
};

pare(userChoice, puterChoice);
// end of rockpapergame.js//
<!DOCTYPE html>
<html lang="en">
    <head>
    <meta charset="utf-8">
    <title>A Little Game</title>
    </head>
    <body>
    <div></div>
    <p><b>About this Game</b></p>
    <script src="rockpapergame.js"></script>
    <p>This is a cool game</p>
    </body>
</html>

The goal is run the game.html, so that it will call the JavaScript and run rockpapergame.js. Using the Chrome Browser, then Inspect, at Sources, I can look at the debugger and step over the function for the referenced js at script src="rockpapergame.js", and step through the rockpapergame.js. However, I get only the prompt for: userChoice = prompt("Do you choose rock, paper or scissors?").

How do I return the console.log output to the HTML5 page?

// Rock, Paper, Scissors Game //
// rockpapergame.js          //

var userChoice = prompt("Do you choose rock, paper or scissors?");
var puterChoice = Math.random();
if(puterChoice < 0.34) {
    puterChoice = "rock";
}else if (puterChoice <= 0.67) {
    puterChoice = "paper";
}else{
    puterChoice = "scissors";
}
console.log("Computer: " + puterChoice);

var pare = function(choice1, choice2)
{
    if (choice1 === choice2) {
      return "The result is a tie!";
    }else if (choice1 === "rock") {
        if (choice2 === "scissors") {
          return "rock wins";
        }else if (choice1 === "paper") {
            if (choice2 === "rock") {
              return "paper wins";
            }else {
              return "scissors wins";
            }
        }else {
          return "paper wins";
        }
    }
};

pare(userChoice, puterChoice);
// end of rockpapergame.js//
<!DOCTYPE html>
<html lang="en">
    <head>
    <meta charset="utf-8">
    <title>A Little Game</title>
    </head>
    <body>
    <div></div>
    <p><b>About this Game</b></p>
    <script src="rockpapergame.js"></script>
    <p>This is a cool game</p>
    </body>
</html>

Share Improve this question asked Oct 25, 2016 at 14:24 TiburonTiburon 551 gold badge1 silver badge6 bronze badges 3
  • I don't understand your question - console.log logs the values in the browser's console, usually used for debugging purposes. If you want to show the result of your game on page, create a named container, and set it's html - for example by creating DOM element <div id='content'></div> and using document.getElementById('content').innerHTML = puterChoice – eithed Commented Oct 25, 2016 at 14:43
  • This sounds like an X-Y problem. What are you really trying to do? – Qix - MONICA WAS MISTREATED Commented Oct 25, 2016 at 17:52
  • The short story is, I need to write an application in XML using js to parse data and return the output to the application using XML. I'm able to parse the data and present using Python 2.7, but need to use XML as presentation method and will use js to parse. I'm learning js and haven't gotten to Data Structures and Objects for JavaScript yet, will hit these in the next two days! As I'm beginning with the basics I wanted to know what the syntax is move data from JavaScript to HTML5. (the back story has much more detail). Sorry to kill this thread with blah, blah blah! – Tiburon Commented Oct 26, 2016 at 17:01
Add a ment  | 

1 Answer 1

Reset to default 5

Following two possible solutions.

01) As others have pointed out in ments, console.log() print always a result in browser console and not in the HTML of your page but you can change its behavior monkey patching (overwriting) the default console.log function provided by the browser, a really simple version could be:

console.log = function(message) {
    document.getElementById('result').innerHTML = message;
};
console.log('your result');
<div id="result"></div>

PS: I personally I would not suggest you to override console.log default behavior.

02) You could add a div container of your result in your HTML page, and use that DIV to show the result for your game.

Modified version from your question:

// Rock, Paper, Scissors Game //
// rockpapergame.js          //

var userChoice = prompt("Do you choose rock, paper or scissors?");
var puterChoice = Math.random();
if(puterChoice < 0.34) {
    puterChoice = "rock";
}else if (puterChoice <= 0.67) {
    puterChoice = "paper";
}else{
    puterChoice = "scissors";
}
document.getElementById('result').innerHTML = "Computer: " + puterChoice;
console.log("Computer: " + puterChoice);

var pare = function(choice1, choice2)
{
    if (choice1 === choice2) {
      return "The result is a tie!";
    }else if (choice1 === "rock") {
        if (choice2 === "scissors") {
          return "rock wins";
        }else if (choice1 === "paper") {
            if (choice2 === "rock") {
              return "paper wins";
            }else {
              return "scissors wins";
            }
        }else {
          return "paper wins";
        }
    }
};

pare(userChoice, puterChoice);
// end of rockpapergame.js//
<!DOCTYPE html>
<html lang="en">
    <head>
    <meta charset="utf-8">
    <title>A Little Game</title>
    </head>
    <body>
    <div></div>
    <p><b>About this Game</b></p>
    <script src="rockpapergame.js"></script>
    <p>This is a cool game</p>
    <div id="result"></div>
    </body>
</html>

发布评论

评论列表(0)

  1. 暂无评论