
add nonuser 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 usersupplied 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.

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.

yeah I don't get it, that's what my teacher posted for the assignment

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

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

Just output "The result is undefined" no matter what the user input is and if your teacher complains, point him to this link.

You could perhaps interpret the problem as being the sum of all integers that can be represented by a 8bit, 16bit or 32bit int (signed or unsigned).

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 email exchanges.
Of course, you still need to try to solve things on your own, and I do want you to email (or even post!) your questions. If that kind of communication seems to be getting you nowhere though, then it is time for an oncampus 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!

Okay, which is what I was getting at in my previous post. So we're talking signed ints, let's say the common 32bit, 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 32bit int (since it will be less than the largest negative number that can be stored). Fortunately, JavaScript doesn't actually use 32bit ints to store numbers, and will not show any signs of breaking.

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?"

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.

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?

Try a smaller range. For example, 10 to 10.

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>

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

Forum Rules

