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 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question how to parseInt and get total from if/else

    I seem to be befuddled on how to take a simple equation and get a total printed onto the alert
    how do I get the total for this? do I need a return? a formula another function? Please help TY
    Sample below:

    function playnice(form) {

    var cat =

    if(cat<=25+1.50)

    else(cat>25*10/100

    alert("Your cats are: " + total)}}

    <input type="text" name="kitty" parseFloat=" ????" value="">

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    You never define what total is.

    And your else is utterly illegal.

    And parseFloat as an <input> attribute doesn't make any sense at all.

    Try showing *REAL* code. That code isn't doing anything at all.
    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.

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    here is the code that I am working on

    I need to get it to add and show it on an alert. I am new to javascript so please be patient. Thank you

    <HTML>
    <HEAD>
    <TITLE>Test Input</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    function testResults (form) {

    var amt = (myform.inputbox.value);
    var total= (amt) += 1.50;
    var s = (amt) *= 10 / 100;
    if(amt<=25 + total) {
    alert("Your Total is $" + total);
    }
    else if(amt>25+s){
    alert("Your Total is now $" + s);
    }
    return;
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM NAME="myform" ACTION="" METHOD="GET">Enter the purchase price: <BR>
    <INPUT TYPE="text" NAME="inputbox" parseInt="" VALUE=""><P>
    <INPUT TYPE="button" NAME="button" Value="Click" onClick="testResults(this.form)">
    </FORM>
    </BODY>
    </HTML>
    Last edited by adshaddd; 07-21-2012 at 07:27 PM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    I am sorry to say that there are many errors in your code. So many in fact that you ought to revise your understanding of this subject - as you say, you seem befuddled. I am not certain even that I have worked out what your calculations are supposed to do.

    Here you are - please study it and learn. It is not really in your best interests that others do your all or most homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase. And naturally there is a limit to the number of times you can take your pitcher to this particular well.

    Code:
    <html>
    <head>
    <title>Test Input</title>
    
    <script type = "text/javascript">
    
    function testResults (form) {  
    var amt = form.inputbox.value;
    var total = Number(amt) + 1.50;  // Must convert input string to a number - add a flat 1.50
    var s = amt * 10 / 100;  // 10% of input amount
    if (total <= 25) {
    alert ("Your Total is $" + total);
    }
    else {  // if total is over 25
    total += s;  // add 10% of input amount
    alert ("Your Total is now $" + total);
    }
    }
    </script>
    </head>
    <body>
    <form action="" >Enter the purchase price: <br>
    <input type ="text" name="inputbox" value =""><br>
    <input type ="button" name="button" value="Click" onClick="testResults(this.form)">
    </form>
    </body>
    </html>
    Note these days HTML tags should be in lower case as future web technologies require this. And the name attribute of <form> is deprecated.

    There is of course no validation of the user input. In real life that would not be acceptable.

    <script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).

    I don't see anything about parseInt() as mentioned in your thread title. But as it says on the tin, parseInt() returns an integer. You want Number().

    “I don't pretend we have all the answers. But the questions are certainly worth thinking about..” - Arthur C. Clarke quotes (English Writer of science fiction, b.1917
    Last edited by Philip M; 07-21-2012 at 08:20 PM.

    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.

  • #5
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    First I would like to start out saying that I do appreciate your help, but I also know that having someone else do my homework is not in my best interest. I find that, according to what you are saying about so much being depreciated, I am being taught wrong. This is very disturbing to me. I will study what you have sent but I will not submit it as my homework, as I have to look at my self in the mirror each day and give account of my doings . I just wanted to know how to put things together.

    Thank you again. Have a nice day.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Quote Originally Posted by adshaddd View Post
    First I would like to start out saying that I do appreciate your help, but I also know that having someone else do my homework is not in my best interest. I find that, according to what you are saying about so much being depreciated, I am being taught wrong. This is very disturbing to me. I will study what you have sent but I will not submit it as my homework, as I have to look at my self in the mirror each day and give account of my doings . I just wanted to know how to put things together.

    Thank you again. Have a nice day.
    You are not the only person who is being taught obsolete Javascript!

    And the word is deprecated - not depreciated. There is a big difference! Like sagacious and salacious.

    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.

  • #7
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    You are not the only person who is being taught obsolete Javascript!

    And the word is deprecated - not depreciated. There is a big difference! Like sagacious and salacious.
    Thank you for your correction

    Since I am being taught obsolete Javascript, do you have any suggestions where to learn the proper way? Maybe through the "World Wide Web Consortium" (W3C)? Or should I find a mentor with vast experience and a ton of patience?
    I also want you to know that learning this is not on a whim or just a school thing. I want to have my own business in Web Design, but at the same time I would like to be able to know ALL of the programming languages. It is a passion not a hobby.
    Thanks again.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    I would like to be able to know ALL of the programming languages.
    Ummm... I have been programming for 43 years. I have used the following programming languages (and probably a few more that I have omitted) [in rough order that I learned them]:
    Fortran II
    SBC BASIC
    COBOL (fundamentals, only)
    Fortran IV
    IBM 360 Assembly Language (AL from here on)
    Interdata 80 AL
    PPS4 AL
    Data General AL
    EDS BASIC
    Algol (fundamentals)
    HP-MX AL
    Various programmable calculator ALs
    Eclipse AL
    Eclipse BASIC
    6800 AL
    6502 AL
    Forth
    NS COPS AL
    Z-80 AL
    Z-BASIC (well, to be fair, I helped create the language)
    Apple II Integer BASIC
    C (by writing a compiler for it)
    Atari BASIC (also helped create it)
    Action!
    BASIC XL
    BASIC XE
    Tiny C
    dBase II
    Pascal
    Prolog
    C++
    dBase IV
    OOSQL
    SQL (4 different varieties thereof)
    Java
    HTML
    VBScript
    JavaScript
    VB.NET
    C#

    And that means I have probably learned maybe 10% of the computer languages that I could have. Maybe. Probably less than 5%, actually.

    I suppose that if all I had ever done was learn computer languages and never actually USED them to create products then I could have learned maybe 50% of the languages. Maybe.

    Then again, maybe not.

    I believe you need to rethink that goal of "ALL languages." Assuming you want to actually earn a living.
    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.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    If you meant the w3cschools site, then no. Most of the JavaScript they teach there is older. Oh, the language, per se, hasn't changed much, but the USAGE of it has.
    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.

  • #10
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I suppose you are right about learning all of it, should have started this right out of College, but life got in the way. I am impressed with how much you know and I don't have 43 years to learn some of what you have learned, but I will keep plugging along.

    It has been a pleasure chatting with you, and thank you for the heads up on learning computer coding. Take care

  • #11
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    @Old Pedant
    You forgot Ruby and Python
    ps. It seem like I forgot some language ... oh F# lol

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    Quote Originally Posted by Richter View Post
    @Old Pedant
    You forgot Ruby and Python
    Nope. I just haven't learned them yet. Started on Python.

    I did forget a few languages in that list. But so obscure nobody would care.

    The most obscure one was assembly language for a custom Atari game microprocessor. I actually had to *first* create the assembly language before I could write the game! It was a weird one. 10 bit ROM (instruction set) and 5 bit RAM (actually, just game registers...no RAM in the conventional sense). I think it only had maybe 16 or 20 different machine instructions. Maybe a few more.
    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
    •