admin管理员组文章数量:1327939
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 usingdocument.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
1 Answer
Reset to default 5Following 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>
本文标签: htmlHow to display consolelog output from JavaScript to HMTL5 pageStack Overflow
版权声明:本文标题:html - How to display console.log output from JavaScript to HMTL5 page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742229336a2436927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论