www.webdeveloper.com
Results 1 to 8 of 8

Thread: New to JavaScript, need some help.

  1. #1
    Join Date
    Jun 2014
    Posts
    3

    New to JavaScript, need some help.

    Hey everyone. I"m really new to Javascript (self teaching). I'm writing some random code for practice, base on the Pokemon gameboy game. I'm doing something wrong, but I"m not sure what it is. The prompt runs with no issue, and I can place the input. But, after that, nothing happens. What am I doing wrong?

    // JavaScript Document
    var Joey=prompt("Youngster Joey wants to battle! Do you accept?").toLowerCase {
    if (Joey===yes) {
    console.log("Excellent. Prepare!");
    }
    else {
    console.log("You can't run from a trainers challenge!");
    }
    }
    var JPokemon=function(math.random() * 2) {
    if (JPokemon===1) {
    console.log("Pidgeot, I choose you!");
    }
    else if(JPokemon ===0) {
    consolelog.("Raticate, I choose you!");
    }
    else {
    console.log("Sandslash, I choose you!");
    }
    };

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,372
    Code:
    .toLowerCase
    needs (); on the end and removal of the trailing {
    Code:
    if (Joey===yes) {
    needs quotes around the yes in the test.
    what would be better is to just test if the answer has a Y in it, eg...
    Code:
    if( Joey.slice(0,1)=="y" ) {
    or
    Code:
    if( Joey.indexOf("y") != -1 ) {

    http://www.w3schools.com/jsref/jsref_obj_string.asp
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Jun 2014
    Posts
    3
    So I made those changes, and I get the same result. I get the prompt, but nothing happens after I put yes, no, whatever, etc. Is there some code that I need to link the functions, that I'm unaware of? Or am I using a ****ty javascript editor?

  4. #4
    Join Date
    Jun 2014
    Location
    in a while loop
    Posts
    6
    I'm not sure what editor you're using but why don't you try something like this? I've added another Pokemon. Hope you don't mind. I made something quick on jsbin:

    http://jsbin.com/jeyal/1/edit?js,output

    Quote Originally Posted by \\.\ View Post
    Code:
    .toLowerCase
    needs (); on the end and removal of the trailing {
    Code:
    if (Joey===yes) {
    needs quotes around the yes in the test.
    what would be better is to just test if the answer has a Y in it, eg...
    Code:
    if( Joey.slice(0,1)=="y" ) {
    or
    Code:
    if( Joey.indexOf("y") != -1 ) {

    http://www.w3schools.com/jsref/jsref_obj_string.asp

  5. #5
    Join Date
    Jun 2014
    Location
    in a while loop
    Posts
    6
    I think I quoted somebody else. Sorry, mean to quote cavendert.

  6. #6
    Join Date
    Jun 2014
    Posts
    3
    Interesting. Your set of code looks completely different. What site/editor are you using? I"m using one that is literally called "free javascript editor". Desktop icon looks like a velociraptor. As far as I can tell, my code is "right". I've used script checks and nothing seems wrong. I'm just not sure why the my code never carries past the prompt. Is there some code that I need to read the input to the prompt? If it helps, I'm learning from codeacademy, so maybe there is something that I just haven't learned yet.

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,372
    Try notepad++ as an editor.

    As for coding, it helps to know what you are trying to achieve.

    The suggestions I made were based on the code you posted, you did not show your modified code, so its impossible to know if you grasped what I was suggesting as possible changes from the options suggested.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  8. #8
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    I made just a few adjustments to your script in order to make it work. If you're still interested in how script works (and where you have made mistakes) go ahead and ask for a detailed explanation.

    Code:
    var Joey=prompt("Youngster Joey wants to battle! Do you accept?").toLowerCase();
    if (Joey==="yes") {
    console.log("Excellent. Prepare!");
    JPokemon();
    }
    else {
    console.log("You can't run from a trainers challenge!");
    }
    function JPokemon() {
    var ranNumber = Math.floor(Math.random() * 3);
    if (ranNumber ===1) {
    console.log("Pidgeot, I choose you!");
    }
    else if(ranNumber === 2) {
    console.log("Raticate, I choose you!");
    }
    else {
    console.log("Sandslash, I choose you!");
    }
    };

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles