www.webdeveloper.com
Results 1 to 15 of 15

Thread: add non-user specified integers?

  1. #1
    Join Date
    Sep 2007
    Posts
    15

    Question add non-user specified integers?

    Ok hi, I'm brand new here and I hope I can get some help. I'm stuck on an assignment in my intro to scripting class. I am to:

    "write a small web page that uses JavaScript to prompt the user for an integer. Verify that they have given you a valid number (i.e. not NaN). If they have entered an invalid value, keep prompting them until they give you a valid number (use a loop for this). Once you have a number, display the sum of all integers that are not the user-supplied number. (For instance, if I entered the value 5, I would see the sum of all integer numbers that are not 5)."

    All I can infer from the assignment description is that the user input will be some sort of variable, and apparently "parseInt" or "parseFloat" will somehow be used to validate whether or not the user input is an integer(correct me if I'm wrong). I understand the use of a loop to repeat the process until a valid integer is given, but I guess where I
    am stuck at is: what the exact variables will be, how to validate an integer, and how to call upon all other integers to add them up. If I could deduct the preceding, I think I could do the rest fine.

  2. #2
    Join Date
    Aug 2007
    Location
    Somewhere in the vicinity of Betelgeuse
    Posts
    194
    The sum of all integer numbers that are not the number? All? I'm no mathematician, but I think it can be shown that the answer would be the negative of the number (if you selected 5 the answer would be -5).

    I suspect, though, that there is an error in the question.
    Not one shred of evidence supports the notion that life is serious.
    eternal.co.za - code, thoughts, rants and raves
    f1rivals.net - formula 1 forums, and, hopefully, soon, prediction game

  3. #3
    Join Date
    Sep 2007
    Posts
    15
    yeah I don't get it, that's what my teacher posted for the assignment

  4. #4
    Join Date
    Jul 2007
    Posts
    20
    is it that u have a set of numbers and if the user selects a number from them, then u add all other numbers except that number

  5. #5
    Join Date
    Sep 2007
    Posts
    15
    It just says as I quoted above, I guess I'll ask. I have only until friday to do this and I'm not sure I'll have much time

  6. #6
    Join Date
    Mar 2005
    Posts
    767
    Just output "The result is undefined" no matter what the user input is and if your teacher complains, point him to this link.
    Stop thinking, start drinking.

  7. #7
    Join Date
    Aug 2007
    Location
    Somewhere in the vicinity of Betelgeuse
    Posts
    194
    You could perhaps interpret the problem as being the sum of all integers that can be represented by a 8-bit, 16-bit or 32-bit int (signed or unsigned).
    Not one shred of evidence supports the notion that life is serious.
    eternal.co.za - code, thoughts, rants and raves
    f1rivals.net - formula 1 forums, and, hopefully, soon, prediction game

  8. #8
    Join Date
    Sep 2007
    Posts
    15
    ok i've got some updates:

    "Now that everyone has suffered a bit I'll pass along a hint or two if necessary.

    First hint: Integers come in both positive and negative values.
    - Well, except for that pesky zero, I think it only comes in negative

    I am not offering this hint because I wish to dissuade anyone from visiting. Far from it! Please feel free to stop by and get help on this or any assignment. I can usually do more good for you during a 20 minute office visit than I can with a dozen e-mail exchanges.

    Of course, you still need to try to solve things on your own, and I do want you to e-mail (or even post!) your questions. If that kind of communication seems to be getting you nowhere though, then it is time for an on-campus intervention."

    "Are you ready? Here is hint number 2:

    An integer variable can only store a number within a range of possible values.

    In other words, the variable cannot store just any integer value. There is a largest possible integer and a smallest possible integer which can be stored."

    thanks everyone for the help!

  9. #9
    Join Date
    Aug 2007
    Location
    Somewhere in the vicinity of Betelgeuse
    Posts
    194
    Okay, which is what I was getting at in my previous post. So we're talking signed ints, let's say the common 32-bit, which can store numbers in the range from 2,147,483,648 to 2,147,483,647. Now every number in the negative range has a positive partner (which cancels it) except 2,147,483,648. Let's say the user specifies a number x then the answer is 2,147,483,648 - x. If x is positive then the answer cannot be stored in a 32-bit int (since it will be less than the largest negative number that can be stored). Fortunately, JavaScript doesn't actually use 32-bit ints to store numbers, and will not show any signs of breaking.
    Not one shred of evidence supports the notion that life is serious.
    eternal.co.za - code, thoughts, rants and raves
    f1rivals.net - formula 1 forums, and, hopefully, soon, prediction game

  10. #10
    Join Date
    Sep 2007
    Posts
    15
    Ok, I've got another update:

    "Hmm, hint number 2 may not have been as clear as I had hoped. I'll add a bit more information to it.

    If an integer variable were one byte long (it is not, this is just a hypothetical), then the range of possible values would be -128 to 127. That range would then be "all the possible integer values" that the variable could store. What would the sum of all those possible integers be?"

  11. #11
    Join Date
    Aug 2007
    Posts
    3,767
    The number is stored as base 2, and a byte is 8 digits long, and the first digit is positive or minus. But two different people have given you the answer, in two totally different ways.
    byte example = 5;
    1 2 3 4 5 6 7 8
    +/- 64 32 16 8 4 2 1
    For an integer it is 32 digits.

  12. #12
    Join Date
    Sep 2007
    Posts
    15
    Ok:

    After staring at this for a while:

    "Hmm, hint number 2 may not have been as clear as I had hoped. I'll add a bit more information to it.

    If an integer variable were one byte long (it is not, this is just a hypothetical), then the range of possible values would be -128 to 127. That range would then be "all the possible integer values" that the variable could store. What would the sum of all those possible integers be?"

    I thought to myself: By that logic wouldn't the sum of the integers be the same value given by the user?

  13. #13
    Join Date
    Aug 2007
    Posts
    3,767
    Try a smaller range. For example, -10 to 10.

  14. #14
    Join Date
    Sep 2007
    Posts
    15
    Yeah, I did. After adding everything from -10 to 10 together you get -10 again but my teacher said I'm still not quite right so then I asked if the answer is: The value given by the user will be returned negative or be returned the same if it's already negative to begin with. I'm still waiting for a response.

    This is what I have:

    <script type = "text/javascript">
    <!--
    var integer, //acceptable integer
    user; //integer entered by user

    user = window.prompt("Enter valid integer");

    integer = parseInt(user);

    if(user != integer) {
    window.prompt("Enter valid integer");
    }

    else
    document.writeln("The sum of all other integers is -" + user + ".");
    // -->
    </script>

  15. #15
    Join Date
    Sep 2007
    Posts
    15
    Oh yeah, I forgot to add, it only loops twice and if it loops and then a valid value is input, the prompt goes away but the message isn't displayed (sorry I'm a total beginner) *scratches head*.

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