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 6 of 6

Thread: Fix my script

  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Fix my script

    So I've just learned how to program in Javascript a few days ago. This is my first time programming anything, so cut me some slack. I've been using codeacademy.com, and I've wrote this script just for fun, but apparently it doesn't work. Can anyone tell my why? (If it's blatantly obvious, it was 1 AM when I wrote this thing...)

    Basically I want it to verify my identity through a password prompt, then ask me if I want to play a game, and if that game is rock paper scissors

    console.log(Initilization confirmed.);
    var password = prompt("User identification. Enter code access authorization password.");
    var initialization = true
    var doodlesLoop = function () {
    while (authorization)
    if ( password !== "ragnarok") {
    prompt("User identity denied. Enter code access authorization password.");
    } else {
    authorization = false;
    console.log(Welcome, sir.);
    }
    }
    doodlesLoop();
    var activity = true;
    var task = prompt("What would you like to do today?");
    if (task === "Play a game") {
    console.log(Of course, sir.);
    var question = prompt ("Shall we play rock, paper, scissors?");
    if (question === "Yes") {
    console.log(Then let us begin.);
    var game = true;
    var gameLoop = function () {
    while (game) {
    var userChoice = prompt("Do you choose rock, paper, or scissors?");
    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
    computerChoice = "rock";
    } else
    if(computerChoice <=o.67) {
    computerChoice = "paper";
    else {
    computerchoice = "scissors";
    }
    }
    console.log("Computer: " + computerChoice);
    var compare = function (choice1, choice2) {
    if (choice1 === choice2) {
    return We have a tie!
    }
    if (choice1 === "rock") {
    if (choice2 === "scissors") {
    return Rock wins!;
    } else {
    return Paper wins!
    }
    }
    if (choice1 === "paper") {
    if (choice2 = "rock") {
    return Paper wins!;
    } else {
    return Scissors wins!
    }
    }
    if (choice1 === "scissors") {
    if (choice2 === "rock") {
    return Rock wins!;
    } else {
    return Scissors wins!
    }
    }
    }
    compare (userChoice, computerChoice)
    var choice = prompt("Do you wish to continue, sir?");
    if (choice === "yes") {
    console.log(Of course, sir.);
    } else {
    game = false;
    }
    }
    gameLoop();
    } else {
    activity = false;
    } else {
    prompt("What would you like to do?");
    }

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,099
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Have you looked at the error console?
    FF or Chrome browsers have helpful debugging sections.
    For example, every one of your "return" statements, like
    return We have a tie!
    is invalid syntax.

    I believe you messages to the console.log need to be enclosed in quote characters also.

    BTW, you might want to add a "please" to your thread title.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,146
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    if ( password !== "ragnarok") {
    Javascript passwords are useless as anyone can see the password with View Source.

    Internet Explorer also has an error console (F12 key).

    You need to have a look at the forum rules and guidelines.

    Code:
    var please = false;
    var thanks = false;
    if (!please && !thanks) {
    var interestLost = true;
    var moreHelp = 0;
    }
    Last edited by Philip M; 08-16-2014 at 07:40 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The latest version of ALL popular browsers has the debugging tools attached to F12.

    Firefox was the last to add built in debugging tools. For versions of IE before IE8 there is a debugging toolbar plugin.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,493 Times in 4,457 Posts
    What nobody mentioned so far is that there are so many logic errors in your code that it's never going to run properly.
    Code:
    var doodlesLoop = function () {
        while (authorization)  // see below 
            if ( password !== "ragnarok") {
                prompt("User identity denied. Enter code access authorization password.");	
            } else {
                authorization = false;
                console.log("Welcome, sir."); // note the added quote marks!
            }
    }
    First of all, you omitted the { } after the initial while in that code. As it happens, this code will run without it, but it's really bad practice to do so.

    But the main problem here is that the variable authorization is never initialized to any value, so it will automatically have an effective value of null.
    And that means that the code while (authorization) becomes while (null)
    which in turn becomes effectively while ( false )
    which, finally, means that the loop never runs. And so the function does NOTHING and returns immediately and no password checking actually takes place, at all.

    In short, you need to pretty much toss out all that code and rewrite it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,493 Times in 4,457 Posts
    Need to start by saying that the use of console.log() and prompt() and alert() should be limited to ONLY the debugging of your code.
    They should NEVER be used in actual web pages.

    That being said, we'll assume that this code is just an exercise in debugging.

    So let's replace ALL of this code
    Code:
    var password = prompt("User identification. Enter code access authorization password.");
    var initialization = true
    var doodlesLoop = function () {
    while (authorization)
    if ( password !== "ragnarok") {
    prompt("User identity denied. Enter code access authorization password.");	
    } else {
    authorization = false;
    console.log(Welcome, sir.);
    }
    }
    doodlesLoop();
    with this code:
    Code:
    while ( true )
    {
        var password = prompt("User identification. Enter code access authorization password.");
        if ( password === "ragnarok" ) 
        {
            console.log( "Welcome, sir." );
            break; // breaks out of while loop and continues after it
        }
        console.log( "Try again.");
    }
    See how simple it can be?
    Last edited by Old Pedant; 08-16-2014 at 07:36 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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