0

I am new to coding but I thought you could help me here. The thing I want is the code to print on the actual webpage, but it doesn't did I something wrong, or did I just misunderstand the console.log code?

   switch(user){
   case 'FIGHT': 
    console.log("You choose to fight the man who robbed you.");
    var fight = prompt("Do you choose your RIGHT HAND or LEFT HAND to hit the theif     with?","Choose you answer here, RIGHT HAND or LEFT HAND?").toUpperCase(); 

    switch(fight) {
    case 'RIGHT HAND':
        console.log("You choose your right hand, the strongest and most buffest arm you have(Yes you are an octopus in this game), you hit the theif and he flies at least one mile away. But you got your stuff back. Congratz!")
    break;

    case 'LEFT HAND': 
        console.log("You choose the left hand, the weakest arm you have. You punch the theif, but instead of damaging him, you break your hand and dies.");
    break;

    default: 
        console.log("You didn't type a legit answer, now the theif killed you. Try       again.")
    break;

    };
  • 3
    Yes, you misunderstood; it logs to the console. Logging to an actual webpage is surprisingly difficult. – Ry- Jul 15 '14 at 22:57
  • Okay, how do i then make it go onto the webpage? Any code i could use? So maybe i should just move on and learn that a bit later? – Mikkel Laursen Jul 15 '14 at 22:57
  • console.log is used for debugging only. – urnotsam Jul 15 '14 at 22:58
  • 1
    That depends on how you want it to go onto the webpage. Is there already a page there, or is all interaction done through `prompt`? – Ry- Jul 15 '14 at 22:58
  • Well, yes it is only operated by prompt. But could i possibly save the code and learn to print it to the webpage? – Mikkel Laursen Jul 15 '14 at 22:59
  • Then you can use `document.write`. `alert` will also work if you want aynchronous (and annoying!) popups. (Also, have the obligatory “don’t do real user interactions like this in browser JavaScript”.) – Ry- Jul 15 '14 at 23:00
  • But I also want it to be so that when you load the page, the page is clean. – Mikkel Laursen Jul 15 '14 at 23:02
  • 1
    Then learn how to view console in your favorite browser. – PM 77-1 Jul 15 '14 at 23:04

2 Answers2

0

console.log outputs to the javascript console. In most browser's this is located somewhere in the developer tools (usually F12).

Update

As in the comments, you can try

document.body.appendChild( document.createTextNode("Your message here") );

courtesy of enhzflep.

or

document.body.innerHTML += "text"

courtesy of Derek

sharf
  • 2,123
  • 4
  • 24
  • 47
  • 1
    `document.write` seriously – Derek 朕會功夫 Jul 15 '14 at 22:59
  • +1 for the mention of how to get to the console, -1 for the mention of `document.write` - make that a 0 overall. Appending text nodes to the document is easy - there's no need to advocate a means to wipe out all existing content in the body. Much better: `document.body.appendChild( document.createTextNode("Your message here") );` – enhzflep Jul 15 '14 at 23:00
  • @Derek朕會功夫 For not using any libraries, and for simply getting text onto the page (given the information OP provided) document.write is perfectly fine. But seeing as you guys don't like it I'll update. – sharf Jul 15 '14 at 23:00
  • 2
    `document.write` is horrible advice... if used after page load it will wipe out the page – charlietfl Jul 15 '14 at 23:00
  • 1
    `document.body.innerHTML += "text"` is enough IMO – Derek 朕會功夫 Jul 15 '14 at 23:02
  • If all the OP wanted was to output text to the page, document.write would do that. There was no context suggesting a better solution was necessary. – sharf Jul 15 '14 at 23:04
  • Do I then need to replace the console.log code with the document.body.appendChild( document.createTextNode("Your message here") );? – Mikkel Laursen Jul 15 '14 at 23:04
  • 1
    @Derek朕會功夫: If you want to inefficiently clobber all existing event listeners, sure. – Ry- Jul 15 '14 at 23:05
  • @MikkelLaursen If you want it to output to the screen, yes. – sharf Jul 15 '14 at 23:05
  • But if I use the document.body.appendChild( document.createTextNode("Your message here") ); does it then delete the text if i reload the page? – Mikkel Laursen Jul 15 '14 at 23:07
  • @minitech - By looking at OP's coding style, I don't think he is attaching events to any elements. But of course in real life situations, don't directly modify the HTML. – Derek 朕會功夫 Jul 15 '14 at 23:07
  • @MikkelLaursen - Yes. – Derek 朕會功夫 Jul 15 '14 at 23:07
  • @MikkelLaursen Yes it will, but such is the nature of HTML manipulated with Javascript. Nothing you do to an HTML page in Javascript is saved unless you do things server-side or with cookies/local storage – sharf Jul 15 '14 at 23:08
  • Well i did attach my jscript to an html document if that was what you meant Derek. – Mikkel Laursen Jul 15 '14 at 23:09
  • Just a last thing, in javascript, can i then perform 2 action fx. prompt("") and document.body.appendChild( document.createTextNode("Your message here") );. If i can how do I need to set it up? – Mikkel Laursen Jul 15 '14 at 23:11
  • @MikkelLaursen What are you trying to accomplish? That may be something to create a new question for. – sharf Jul 15 '14 at 23:14
  • I am trying to make it prompt a message which the user have to answer, and when they have answered that question i want it to print the question to the webpage. – Mikkel Laursen Jul 15 '14 at 23:16
  • @MikkelLaursen then do `var user = prompt()` and then do your switch statement below the prompt. – sharf Jul 15 '14 at 23:21
  • @sharf When i use that tag google chrome gives me this error in the console : uncaught typeerror: cannot read property'appendChild' of null
    `case 'RUN': console.log("You choose to try and run away."); document.body.appendChild( document.createTextNode("You choose to try and run away."));`
    – Mikkel Laursen Jul 15 '14 at 23:33
  • @MikkelLaursen that doesn't sound right, that would mean `body` is undefined. Can you show us the line of code that is throwing the error? – sharf Jul 15 '14 at 23:37
  • @MikkelLaursen There is nothing wrong with that line that I can tell. Can you please update your answer with your new code, and include as much HTML and Javascript as possible? – sharf Jul 15 '14 at 23:43
  • @sharf well i think i will just screw it, now i don't know what happened but it doesn't work. But after all I thank you for taking your time to help me and you did learn me something new, console.log doesn't print on sites :D. So yeah, I'll properly return yet another time with a different problem. Have a nice night/day. – Mikkel Laursen Jul 15 '14 at 23:47
-3

console.log outputs messages to browser console, that is accessible by pressing F12 in Google Chrome, for example. To output text on page, you probably have to use something like jQuery, KnockoutJS or other databindings libraries

vodolaz095
  • 6,680
  • 4
  • 27
  • 42