Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help me with a totally cinch "input/output" problem

    can anyone here please help me embed this little piece of javascript into an HTML file?

    I've been giving it a try myself but it won't take "input" from me. rather it just triggers the ans right away the second I open my HTML page.
    I know there has to be something with "window.onload = init;" but I'm quite confused. any kind of help woud be greatly appriciated.


    P.S: I'm just a studnet and I've barely attended my 2nd day class yet


    Code:
     var guessInput = document.getElementById("guess");
    var guess = guessInput.value;
    var answer = checkGuess(guess);
    alert(answer); 
    
    
    function checkGuess(guess) {
    var answers = [ "red",
    "green",
    "blue"];
    var index = Math.floor(Math.random() * answers.length);
    if (guess == answers[index]) {
    answer = "You're right! I was thinking of " + answers[index];
    } else {
    answer = "Sorry, I was thinking of " + answers[index];
    }
    return answer;
    }

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,243
    Thanks
    12
    Thanked 340 Times in 336 Posts
    rather it just triggers the ans right away the second I open my HTML page.
    then you need to tie the execution to a user supplied event (clicking an element, etc.)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script>
    function makeGuess(){
    	var guessInput = document.getElementById("guess");
    	var guess = guessInput.value;
    	if(!guess){return;}
    	var answer = checkGuess(guess);
    	alert(answer);
    	guessInput.options.selectedIndex = 0;
    }
    
    function checkGuess(guess) {
    	var answers = ["red", "green", "blue"], answer;
    	var index = Math.floor(Math.random() * answers.length);
    	if (guess == answers[index]) {
    		answer = "You're right! I was thinking of " + answers[index];
    	} else {
    		answer = "Sorry, I was thinking of " + answers[index];
    	}
    	return answer;
    }
    </script>
    </head>
    <body>
    <select id="guess" onchange="makeGuess();">
    <option value="">Guess a Color</option>
    <option value="red">red</option>
    <option value="green">green</option>
    <option value="blue">blue</option>
    </select>
    </body>
    </html>
    - John


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •