PDA

View Full Version : Javascript Pop-up Calculator

kyle7560
Oct 23rd, 2009, 06:50 AM
aaaaaa

Philip M
Oct 23rd, 2009, 07:51 AM
Is this homework or a student assignment? If so, please refer to the forum Rules.

kyle7560
Oct 23rd, 2009, 08:15 AM
aaaaaaa

kyle7560
Oct 23rd, 2009, 08:28 AM
aaaaaaaa

Philip M
Oct 23rd, 2009, 08:34 AM
This will give you a (considerable) start. All you have to do now is sum the numbers in the array using a loop and calculate the average.

<script type = "text/javascript">

var numNums = parseInt(prompt("How many numbers do you want to sum up?",""));
var nums = new Array(numNums);
for (var i=0; i<numNums; i++) {
var ans = parseFloat(prompt ("Enter a number to be summed", ""));
nums[i] = ans;
if ((isNaN(ans)) || (ans == "")) {
alert ("You must enter a number!! ");
i -- ;
}
}
alert (nums); // remember that an array index starts at 0. You refer to the numbers as nums[0], nums[1] etc.
</script>

Why do you "have no idea where to start"? Surely this is not hard if you have been following the class.
You may wish to check also that the entered value of numNums is an integer number. Just apply the same code as above.

"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

kyle7560
Oct 23rd, 2009, 08:43 AM
aaaaaaa

Philip M
Oct 23rd, 2009, 08:57 AM
Thanks a lot. Up to this point in the class everything has made sense but for some reason I'm having problems with this section. None of the material that we've read has even mentioned arrays.

Here is an alternative without using an array which calculates the (running) total but of course the individual values are not preserved. I trust that you can add the one line required to calculate the average. It would be a good idea if you told your tutor that you had received considerable help on the internet - otherwise he might form an incorrect opnion as to your prowess.

<script type = "text/javascript">
var numtotal = parseInt(prompt("How many numbers do you want to sum up?",""));
var total = 0;
for (var i=0; i<numtotal; i++) {
var ans = parseFloat(prompt ("Enter a number", ""));
if ((isNaN(ans)) || (ans == "")) {
alert ("You must enter a number!! ");
i -- ;
}
else {total = total + ans};
}
</script>

kyle7560
Oct 23rd, 2009, 12:04 PM
aaaaa

Philip M
Oct 23rd, 2009, 12:19 PM
var again=prompt("Do you want to do another calculation (Y/N)?", "");
if (again.toLowerCase() == "y") {runJS()}

I see no purpose in the random number which does nothing.

kyle7560
Oct 23rd, 2009, 12:22 PM
aaaaa

Old Pedant
Oct 23rd, 2009, 11:10 PM
I think Kyle is scared his teacher is going to see this thread and know he didn't do his own work.

Can we call a sanction on him for destroying all his posts and making the thread worthless?? Or do we let it go one time?

jmrker
Oct 24th, 2009, 03:36 AM
aaaaa

What the H*** are you trying to ask???

The best response I could give you is "bbbbb" in answer to any of your posts!

Philip M
Oct 24th, 2009, 08:58 AM
I think Kyle is scared his teacher is going to see this thread and know he didn't do his own work.

Can we call a sanction on him for destroying all his posts and making the thread worthless?? Or do we let it go one time?

As I understand it, it is an infraction-worthy offence to delete posts for no good reason - perhaps 12 Pack Mack would confirm. But the thread is not now worthless - you can still see my most admirable solutions even if the questions have gone. :cool:

I believe that the mods can restore deleted posts, and in this case I invite them to do so.

jmrker
Oct 24th, 2009, 01:49 PM
As I understand it, it is an infraction-worthy offence to delete posts for no good reason - perhaps 12 Pack Mack would confirm. But the thread is not now worthless - you can still see my most admirable solutions even if the questions have gone. :cool:

I believe that the mods can restore deleted posts, and in this case I invite them to do so.

While your coding efforts may be worthy of the 'coders hall of fame' :thumbsup:
I'm still trying to figure out what the question was ... :D :D :D :D

Philip M
Oct 24th, 2009, 04:00 PM
All he wanted was a script for the user to be prompted to enter several numbers, then calculate the sum and the average.

jmrker
Oct 24th, 2009, 05:43 PM
All he wanted was a script for the user to be prompted to enter several numbers, then calculate the sum and the average.

Thanks ... that's exactly what your code appears to do.

Sounds like homework to me. :eek: :rolleyes: :D :) :thumbsup:

Philip M
Oct 24th, 2009, 05:52 PM
Thanks ... that's exactly what your code appears to do.

Sounds like homework to me. :eek: :rolleyes: :D :) :thumbsup:

Well, he admitted that. See Post#2. But he had made an effort to write the script himself, albeit without too much idea of what he was doing.