Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Simple Adding Calculator - Javascript

  1. #1
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Simple Adding Calculator - Javascript

    Hi. I have a very simple adding calculator to do written in JS. Can anybody please help me fill in the blanks please?

    The end result should look like the attached screenshot and on pressing "Calculate", I would like an alert box to appear showing the result.

    The code must be the simplest possible and MUST include the parseInt() function.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <title>Assignment 2 - Exercise 3</title>
    </head>

    <body>

    <script language="JavaScript">

    var greeting;
    greeting = "Hello. Please enter the numbers you would like added together.";
    window.alert(greeting);

    var myInt;
    var myFloat;

    myInt = parseInt(number1);
    document.write(" ????????? ");

    myFloat = parseFloat(number2);
    document.write(" ????????? ");
    </script>

    <table width="45%" align="left" border="2" cellpadding="8">
    <tr>
    <td>
    <h1>The Adding Calculator! </h1>

    <form name="form1">
    Enter some numbers in these boxes:

    <center>
    <p>

    <input type="text" size="20" maxlength="20" name="number1" value="0">
    &nbsp;
    +
    &nbsp;
    <input type="text" size="20" maxlength="20" name="number2" value="0">
    &nbsp;
    =
    &nbsp;
    <input type="button" name="calculate" value="CALCULATE" onClick=" ???????????? ">

    </form>
    </center>

    </td></tr>
    </table>

    </body>
    </html>

    Thank you as always. s

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>The Adding Calculator</title>
      </head>
      <body>
        <h1>The Adding Calculator</h1>
        <div>
          <form action="" onsubmit="this.elements.total.value = +this.elements.n1.value + (+this.elements.n2.value);">
            <label>
              First number:
              <input type="text" name="n1">
            </label>
            <label>
              Second number:
              <input type="text" name="n2">
            </label>
            <label>
              Total:
              <input type="text" name="total">
            </label>
            <input type="submit" value="Calculate">
          </form>
        </div>
      </body>
    </html>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franįais | entiendo espaņol | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    you probably should read in your book to figure out these homework problems. coming here and having someone else writing a script for you will not make you learn what you are doing, it will only show you one of many possible solutions. if you have a specific question about something that is one thing, but having one of us consistently do your homework will not benefit you in any way.

  4. #4
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Yes, I understand your point but I wouldn&#180;t come here in the first place if I could understand my book. Believe it or not, I do spend hours trying to work it out myself before coming on here and asking for help. Thank you for your help so far, I&#180;ll try not to bother you again.

  5. #5
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>The Adding Calculator</title>
      </head>
      <body>
        <h1>The Adding Calculator</h1>
        <div>
          <form action="" onsubmit="this.elements.total.value = +this.elements.n1.value + (+this.elements.n2.value);">
            <label>
              First number:
              <input type="text" name="n1">
            </label>
            <label>
              Second number:
              <input type="text" name="n2">
            </label>
            <label>
              Total:
              <input type="text" name="total">
            </label>
            <input type="submit" value="Calculate">
          </form>
        </div>
      </body>
    </html>
    Twey, is there a way it will work without using the DIV and LABEL tags? I don&#180;t know what they are / haven&#180;t studied those yet. Thank you.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    div = division, a separate region on the page.
    They don't affect functionality, unless they relate to a script in some way, such as being a region in which something is displayed.
    In this case, it's just an organized way to form the page.

    label is a tag that surrounds text and a form element.
    <label>click me<input type="textbox"></label>, for example.
    if you click "click me", the checkbox will select, like if you had clicked in the box itself.
    Above, there is no specific functionality for the tags, but they are a good way to assign.... a label. The layout/style can be customized easily with CSS, such as if the labels on the page should be bold.
    Labels also, in some cases, may bring focus (the state of being active) to a form element in some cases. Not entirely sure on that.


    In short, no, those aren't required for the script at all. (But you'll have to be careful to remove them properly if you choose to do so.)

    It's completely understandable that you are just starting out and this stuff can be confusing at first. However, for such basic questions, you really should be able to look them up on google.
    With html, javascript, etc., each tag (or property in JS) has a specific function. <p> has a specific function. And so would <abcdef>.

    the w3schools site is quite good for tutorials and reference. I suggest starting there.
    http://w3schools.com/



    EDIT: Stupid typo. "screen" before should have been "script"
    Last edited by djr33; 09-13-2007 at 05:09 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| espaņol | Deutsch | italiano | portuguęs | catalā | un peu de franįais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thank you djr33 for explaining that to me - it makes it so much clearer now. Itīs not the same reading something and then having it explained from someone who has experience.

    I did used to search everything on google - thatīs how I came across this site, decided it was the best resource I could find and stuck with it.

    I will try not to use it so much from now on though.

    Thanks again. S

  8. #8
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by susie123 View Post
    Thank you for your help so far, I&#180;ll try not to bother you again.
    Quote Originally Posted by susie123 View Post
    I will try not to use it so much from now on though.
    its not that we do not want to help you, so please do not think that. its just that if this is an assignment for school, we shouldn't be giving you the whole solution, but rather helping ammend your code or explain as djr did the concepts behind the solution and that way you can understand them more thoroughly which would enable you to learn the material and also get your work done on time.

    if you are having trouble understanding some of the concepts you also might want to talk to your teacher / professor... that is their job, to help you understand the concepts behind the techniques you are learning. there are many very knowledgable persons here who are willing to help, otherwise we wouldn't be here, so please do not be discouraged of coming back.

  9. #9
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Thumbs up

    Ok Boogyman, thank you. The problem I have is there is no teacher - itīs a distance-learning course on CD that Iīm following on my own (want a change of career at 34) and I seem to get answers back a lot quicker from this site than from the email contact I have for the course.

    I really do appreciate all the help from all of you.

    Itīs strange - I have one of those minds that can pick up languages (the spoken kind) pretty quickly but when it comes to anything mathematical, all sense disappears. Iīm sure Iīll get used to it though.

    Thanks again

    S

  10. #10
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    i am just the opposite. I am good with numbers and science, however when it comes to spoken languages, I cannot retain the information as quickly or thoroughly. anyway good luck with your new career.

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
  •