Click to See Complete Forum and Search --> : Need Help Finding Unknow Integers
lodge308
11-01-2003, 07:53 PM
I have an assignment where I need to have the user enter two integers. The program will then display all of the even numbers within the two integers entered. I know that I will need to use a modulo to find which numbers are even. I just can't figure out how to get the program to find the numbers between the two entered to begin with. I can't decide odd or even when I don't know what the numbers are. Any help would be appreciated. :confused:
This is in Javascript...
gil davis
11-01-2003, 08:30 PM
Assume a form named "f1" with fields named "n1", "n2" and "ans":
var ans = "";
var n1 = parseInt(document.f1.n1.value);
var n2 = parseInt(document.f1.n2.value);
for (var i=n1; i<=n2; i++) {
if (i % 2 == 0)
{ans = (ans == "") ? ans + "" + i : ans + ", " + i;}
}
document.f1.ans.value = ans;
zachzach
11-01-2003, 08:36 PM
umm try this:
pretent variable "A" = first integer, and variable "B" = second integer
<script>
evennumbers = "";
for(i=A;i<B;i++) {
if(i/2==Math.round(i/2)) {
if(evennumbers == "") {
evennumbers = evennumbers + "" + i
}else {
evennumbers = evennumbers + " and " + i
}
}
}
document.write(evennumbers)
</script>
but if its an asignment, arn't you supposed to right the code?well, anyway thats your bznes
(ask someone to check the capatalazation, cuz in javascript it gets pissed if you 'cap something wrong(dont ask me, ask the guys who made it!))
please join my forums, i need members+modorators!
zachzach
11-01-2003, 08:52 PM
by the way, i know mine works, and im pretty sure his does 2, without testing it, so i guess whichever you want is what you will pick 'cuz both ways works(wow that was
(pointless == true) ? "pointless" : "not a waste of time"
)
lodge308
11-03-2003, 09:58 PM
Thank you both for your input. The code wasn't as important as being able to grasp it conceptually. I was able to make some progress. Thanks again.:)
zachzach
11-04-2003, 01:38 PM
Welcome, good luck on ur assignment(lol)