SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Convert inputs:

  1. #1
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)

    Convert inputs:

    Code JavaScript:
    <script type="text/javascript">
    function pFIdVal(id){return parseFloat(document.getElementById(id).value);}
    function calculate(){
    var v1=pFIdVal('mycredits')/1000000;
    var v2=pFIdVal('mybytes')/5;
    var v3=pFIdVal('mymarks')/1000;
    var v4=pFIdVal('myexp')/1000000;
    var v5=pFIdVal('myarena')/100;
    var v6=pFIdVal('myclearing')/20;
    var v7=pFIdVal('myweapons')/155000;
    var v8=pFIdVal('myore')/2000;
    var v9=pFIdVal('myherbs')/2600000;
    var tG=v1+v2+v3+v4+v5+v6+v7+v8+v9;
    document.getElementById('answer').innerHTML=tG;
    var iR;
     
    if(tG<1){iR='<span><h1>Maintenance Guy</h1><p>This is the entry level rank for all new applicants.</p><h2>Powers:</h2><p>This rank has no powers.</p></span>';}
     
    if(tG>=1&&tG<3){iR='<span><h1>Cook</h1><h2>Powers:</h2><p>This rank has no powers.</p></span>';}
     
    if(tG>=3&&tG<5){iR='<span><h1>Mechanic</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=5&&tG<10){iR='<span><h1>Volunteer</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=10&&tG<20){iR='<span><h1>Recruit</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=20&&tG<30){iR='<span><h1>Private</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=30&&tG<40){iR='<span><h1>Private First Class</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=40&&tG<50){iR='<span><h1>Sniper</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=50&&tG<60){iR='<span><h1>Tank Driver</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=60&&tG<100){iR='<span><h1>Airman</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=100&&tG<120){iR='<span><h1>Sergeant</h1><h2>Powers:</h2><p>- Accept/deny applicants.<br />- Modify invite list.<br />- View recruitment   tool.<br />- Can modify blacklist.<br /> - Full Sync.</p></span>';}
     
    if(tG>=120&&tG<150){iR='<span><h1>Warrant Officer</h1><h2>Powers:</h2><p>- Accept/deny applicants.<br />- Modify invite list.<br />- Repair equipment.<br />- View recruitment tool.<br />- Can modify blacklist.<br />- Full Sync.</p></span>';}
     
    if(tG>=150&&tG<200){iR='<span><h1>Lieutenant</h1><h2>Powers:</h2><p>- Accept/deny applicants.<br />- Modify invite list.<br />-   Ban/unban members from MB/view post aaatml.<br />- Repair equipment.<br />- View   recruitment tool.<br />- Can modify blacklist.<br />- Full Sync.</p></span>';}
     
    if(tG>=200&&tG<300){iR='<span><h1>Captain</h1><h2>Powers:</h2><p>- Accept/deny applicants.<br />- Modify invite list.<br />- Modify ban list.<br /- Ban/unban members from MB/view post aaatml.<br />- Repair   equipment.<br />- View recruitment tool.<br />- Can modify blacklist.<br />- Full   Sync.</p></span>';}
     
    if(tG>=300){iR='<span><h1>Secret Agent</h1><p>You are the ****.</p><h2>Powers:</h2><p>Needs declared.</p></span>';}
     
    document.getElementById('rank').innerHTML=iR;
    }
    </script>

    I would love it if I could accomplish two things here. 1) If you leave a field blank, it converts it to 0 so that you don't get an error. 2) If someone types in 10k it would be equal to 10000, and same for 10m, it would equal 10000000

    Thanks much.

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,345
    Mentioned
    192 Post(s)
    Tagged
    5 Thread(s)
    You need to modify the:
    function pFIdVal(id){return parseFloat(document.getElementById(id).value);}

    Parsing the numerous possibilities of entry that could be made (ie 10K or 1,000) would be best accomplished using RegExp. A google search will, likely, yield a great routine for that purpose.
    To help you with #1 on your list I suggest the following:

    function pFIdVal(id){
    idVal = document.getElementById(id).value;
    return (idVal == "") ? 0 : parseFloat(document.getElementById(id).value);
    }

    Hope this helps.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Member vidalstat's Avatar
    Join Date
    Dec 2007
    Location
    Brooklyn NY
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reg exp

    something like this might work to replace 10k to 10,000

    Code:
    function parse_input(str){
    var re = /[K|k]/;
    str = str;
    nstr = ",000"
    var result = str.replace(re,nstr);
    return result;
    }
    
    parse_input(10K);

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Code JavaScript:
    <script type="text/javascript">
    function pFIdVal(id){
    idVal = document.getElementById(id).value;
    return (idVal == "") ? 0 : parseFloat(document.getElementById(id).value);
    }
    function calculate(){
    var v1=pFIdVal('mycredits')/1000000;
    var v2=pFIdVal('mybytes')/5;
    var v3=pFIdVal('mymarks')/1000;
    var v4=pFIdVal('myexp')/1000000;
    var v5=pFIdVal('myarena')/100;
    var v6=pFIdVal('myclearing')/20;
    var v7=pFIdVal('myweapons')/155000;
    var v8=pFIdVal('myore')/2000;
    var v9=pFIdVal('myherbs')/2600000;
    var tG=v1+v2+v3+v4+v5+v6+v7+v8+v9;
    document.getElementById('answer').innerHTML=tG;
    var iR;
     
    if(tG<1){iR='<span><h1>Maintenance Guy</h1><p>This is the entry level rank for all new applicants.</p><h2>Powers:</h2><p>This rank has no powers.</p></span>';}
     
    if(tG>=1&&tG<3){iR='<span><h1>Cook</h1><h2>Powers:</h2><p>This rank has no powers.</p></span>';}
     
    if(tG>=3&&tG<5){iR='<span><h1>Mechanic</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=5&&tG<10){iR='<span><h1>Volunteer</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=10&&tG<20){iR='<span><h1>Recruit</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=20&&tG<30){iR='<span><h1>Private</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=30&&tG<40){iR='<span><h1>Private First Class</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=40&&tG<50){iR='<span><h1>Sniper</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=50&&tG<60){iR='<span><h1>Tank Driver</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=60&&tG<100){iR='<span><h1>Airman</h1><h2>Powers:</h2><p>- Modify invite list.<br />- View recruitment tool.</p></span>';}
     
    if(tG>=100&&tG<120){iR='<span><h1>Sergeant</h1><h2>Powers:</h2><p>- Accept/deny applicants.<br />- Modify invite list.<br />- View recruitment   tool.<br />- Can modify blacklist.<br /> - Full Sync.</p></span>';}
     
    if(tG>=120&&tG<150){iR='<span><h1>Warrant Officer</h1><h2>Powers:</h2><p>- Accept/deny applicants.<br />- Modify invite list.<br />- Repair equipment.<br />- View recruitment tool.<br />- Can modify blacklist.<br />- Full Sync.</p></span>';}
     
    if(tG>=150&&tG<200){iR='<span><h1>Lieutenant</h1><h2>Powers:</h2><p>- Accept/deny applicants.<br />- Modify invite list.<br />-   Ban/unban members from MB/view post aaatml.<br />- Repair equipment.<br />- View   recruitment tool.<br />- Can modify blacklist.<br />- Full Sync.</p></span>';}
     
    if(tG>=200&&tG<300){iR='<span><h1>Captain</h1><h2>Powers:</h2><p>- Accept/deny applicants.<br />- Modify invite list.<br />- Modify ban list.<br /- Ban/unban members from MB/view post aaatml.<br />- Repair   equipment.<br />- View recruitment tool.<br />- Can modify blacklist.<br />- Full   Sync.</p></span>';}
     
    if(tG>=300){iR='<span><h1>Secret Agent</h1><p>You are the ****.</p><h2>Powers:</h2><p>Needs declared.</p></span>';}
     
    document.getElementById('rank').innerHTML=iR;
    }
    </script>

    Where would that go in there? And while Im thinking of it. How about getting it to accept , in there. Example: 10,000 equals 10000

  5. #5
    SitePoint Member vidalstat's Avatar
    Join Date
    Dec 2007
    Location
    Brooklyn NY
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    inputs

    You can convert 10,000 to 10000, with another regular expression:

    Code:
    re = /,/;
    str = "";
    pattern = "10,000";
    
    pattern.replace(re,str);
    this will find all commas and remove them.

    You can then parseInt() or parseFloat() the resulting pattern and use it in calculations.

    as far as where you would use the previous function, I would use it as soon as you get the input from the user run the input through that function and reset the variable like this:

    var input = parse_input(input);


Bookmarks

Posting Permissions

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