admin管理员组文章数量:1394057
I have some functions and some variables. I would like to return a variable and the function oute as text on my browser.
What I have done is I have made a HTML file with the text:
<SCRIPT SRC="rockpaper.js">
</SCRIPT>
And this refers to this javascript file:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var puterChoice = Math.random();
if (puterChoice < 0.34) {
puterChoice = "Computer chooses rock";
} else if(puterChoice <= 0.67) {
puterChoice = "Computer chooses paper";
} else {
puterChoice = "Computer chooses scissors";
}
console.log(puterChoice);
var pare = function(choice1,choice2)
{
if(choice1===choice2)
{
return("The result is a tie!");
}
if(choice1==="Computer chooses rock")
{
if(choice2==="scissors")
{
return("rock wins");
}
else
{
return("paper wins");
}
}
if(choice1==="Computer chooses paper")
{
if(choice2==="rock")
return("paper wins");
else
{
return("scissors wins");
}
}
if(choice1==="Computer chooses scissors")
{
if(choice2==="rock")
{
return("rock wins");
}
else
{
return("scissors wins");
}
}
}
console.log(pare(puterChoice,userChoice))
However, when I open it with a browser, the text doesn't display, but the prompt does.
It works fine in Codecademy, though.
I have some functions and some variables. I would like to return a variable and the function oute as text on my browser.
What I have done is I have made a HTML file with the text:
<SCRIPT SRC="rockpaper.js">
</SCRIPT>
And this refers to this javascript file:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var puterChoice = Math.random();
if (puterChoice < 0.34) {
puterChoice = "Computer chooses rock";
} else if(puterChoice <= 0.67) {
puterChoice = "Computer chooses paper";
} else {
puterChoice = "Computer chooses scissors";
}
console.log(puterChoice);
var pare = function(choice1,choice2)
{
if(choice1===choice2)
{
return("The result is a tie!");
}
if(choice1==="Computer chooses rock")
{
if(choice2==="scissors")
{
return("rock wins");
}
else
{
return("paper wins");
}
}
if(choice1==="Computer chooses paper")
{
if(choice2==="rock")
return("paper wins");
else
{
return("scissors wins");
}
}
if(choice1==="Computer chooses scissors")
{
if(choice2==="rock")
{
return("rock wins");
}
else
{
return("scissors wins");
}
}
}
console.log(pare(puterChoice,userChoice))
However, when I open it with a browser, the text doesn't display, but the prompt does.
It works fine in Codecademy, though.
Share Improve this question asked Dec 1, 2013 at 6:48 PazePaze 1831 gold badge1 silver badge11 bronze badges 6- Have you opened up your browser console? The text should be displayed there. In case you are using Google Chrome, look for the console in Tools / Developer tools. – bagonyi Commented Dec 1, 2013 at 6:50
- console.log() outputs to your browser console (hit f12 to bring it up). If you want to display the text to a browser window, you need to assign the return value to a var, then do document.write(var); – ryanlutgen Commented Dec 1, 2013 at 6:51
- How would I assign the return value to a var? Thank you. – Paze Commented Dec 1, 2013 at 7:17
- @BjarniJóhannsson if you do not know what a var is or how to use it then maybe you're starting to learn javascript in the wrong place. – iConnor Commented Dec 1, 2013 at 7:20
- I almost got it. Using document.write() I have gotten my two sentences together in the browser, but they are written directly after each other. How do I add an indent between them? (like if I pressed enter, not sure what it's called). – Paze Commented Dec 1, 2013 at 7:22
1 Answer
Reset to default 2Nothing being displayed on the page is normal behaviour seeing as you have not told the browser to do so.
Maybe you want something like this.
document.body.innerHTML = pare(puterChoice, userChoice);
Basically, this set's the HTML of the body to value and removes anything currently in the body, or
var generatedText = pare(puterChoice,userChoice), // 1
myText = document.createTextNode( generatedText ); // 2
document.body.appendChild( myText ); // 3
Will on line 1 get the generated value and save it, on line 2 will create a text element on the document and on line 3 it will append the text element/node to the end of the body.
This way nothing is removed from the document.
本文标签: How to display text in JavascriptStack Overflow
版权声明:本文标题:How to display text in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744631513a2616568.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论