How can i use these codes on different browsers?


#1

What do I do to make these codes work on chrome or firefox?
please help!!


#2

Do you mean aside from the exercise? Well you need to build an html file and include your script via the script tag and you need to keep in mind that console.log writes to the console and not to the screen. So better have a look at the HTML+CSS and maybe the jQuery track as well.


#3

so for writing onto the screen I use document.write()?


#4

n ya I meant to just write this separtely onto an html file and test it.


#5

@haxor789 can u plz help me with this?? PLEASE?


#6

Here is outputting to a browser explained:

http://www.w3schools.com/js/js_output.asp

document.write() is one way, there are new ways to output javascript in HTML5 as well.


#7

i dont want to know the outputting part!
but how to run the codes in codecdemy on my own browser.


#8

In that case what you want to do is read on how to output javascript into HTML and you should read that link.


#9

okay thanks.
read that olredy.


#10

javascript does not have a built in display function because javascript is interpreted by a browser. In order to display it in a browser you have to pass it into an HTML file.

There are multiple ways of doing it but yes document.write() function is one of those ways.

Do you have a script you want to output? I can help you output the answer if you like.


#11

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare= function(choice1, choice2){
if(choice1===choice2){
return"The result is a tie!";
}
if(choice1 === "rock")
{
if( (choice2 === "scissors")){

                return( "rock wins");}
            else {var userChoice = prompt("Do you choose rock, paper or scissors?");

var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

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

};
return( "paper wins");
}
}
else
{
if(choice1 === "paper") {

                if(choice2 === "scissors" ){

                    return( "scissors wins");}
                else {
                        return( "paper wins");
                        } 
            }
            else
            {
             if(choice1 === "scissors" ) {
                 
                 if(choice2 == "paper")

                    return( "scissors wins");}
                else {
                        return( "rock wins");
                        }    
                }
                            


        }

}


#12

thanks for the help!
just try if u could sort things!

its a code for rock paper scissors!


#13

I couldn't get your code to work but I re-wrote it, it's not the best but it works somewhat for demonstration purposes.

You would have to copy and paste this code into notepad and save as HTML then open in firefox.
Notice i used the method getelemetnbyID(), it's a built in method and used that to replaced the

tags in the html file with the output.

<!DOCTYPE html>
<html>
<body>

<p id="pcChoice"></p>
<p id="answer"></p>

<script>
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} document.getElementById("pcChoice").innerHTML=("Computer: " + computerChoice);

var compare = function(choice1,choice2){
    if (choice1 === choice2){
        userChoice = prompt("Tie, Please try again: Do you choose rock, paper or scissors?");
    } 
    else if (choice1 === "rock"){
        if (choice2 === "scissors"){
            return "rock wins";
        } else {
            return "paper wins";
        }
    } 
    else if (choice1 === "paper"){
        if (choice2 === "rock"){
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors"){
        if (choice2 === "paper"){
            return "scissors wins";
        } else {
            return "rock wins";
        }
    }
};

var whoWins=compare(userChoice,computerChoice);

document.getElementById("answer").innerHTML=(whoWins);
</script>

</body>
</html>

#14

Thanks for answering the question, and innerHTML is much better than document.write() :smile:

@codesolver31872 is this what you searched for or what was your goal?


#15

Hey!!
Thanks alot for solving the problem!!


#16

Ya.I wanted this only and @conolyb1 solved the problem!
Thanks!!
Help highly appreciated! :smile: