User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 402,982 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,686 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1015 | Replies: 17
Reply
Join Date: Apr 2008
Posts: 12
Reputation: stan.joe1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stan.joe1 stan.joe1 is offline Offline
Newbie Poster

Re: Lost by tried and tried

  #11  
Jul 23rd, 2008
Originally Posted by Troy III View Post
OK
try: alert("The Sum is:" + total + "; The average is: " + average); , to alert them both in one turn.

try:
var largest = Math.max(firstnumber, secondnumber, thirdnumber);
var smallest = Math.min(firstnumber, secondnumber, thirdnumber);

alert("smallest:" + smallest + "; largest: " + largest )
to remove the junk you use for this simple task: meaning following:
if(firstnumber > secondnumber && firstnumber > thirdnumber){
var largest= firstnumber;

}
else if(thirdnumber > firstnumber && thirdnumber > secondnumber){
var largest= thirdnumber;
}
if(firstnumber < secondnumber && firstnumber < thirdnumber){
var smallest= firstnumber;
}
else if(secondnumber < firstnumber && secondnumber < thirdnumber){
var smallest= secondnumber;
taht can't posibly be working!




Ok! I am almost there. I successfully manage to add the product bit. There seems to be a problem with the smallest bit I have to enter the follwing numbers in this order 89;44;20 a
This outputs 44 as the smallest if I change the order 20;44;89 then it outputs 20.

And most importantly all the outputs have to be one under the other I tried adding <br> and <p> tags but this dose not work.

And thanks for not giving up on me!

<script LANGUAGE="JAVASCRIPT">
var firstnumber = prompt("Enter first number and Click OK", "0");
var secondnumber = prompt("Enter second number and Click Ok", "0");
var thirdnumber = prompt("Enter third number and Click Ok", "0");
var total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
var average= (Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber))/3;
var product =Math.floor(firstnumber)* Math.floor(secondnumber)* Math.floor(thirdnumber);
var largest = Math.max(firstnumber, secondnumber, thirdnumber);
var smallest = Math.min(firstnumber, secondnumber, thirdnumber);
alert("Sum:" + total +"Average: " + average+ "; Product: " + product+ ";largest: " + largest + "; smallest:" + smallest)
</SCRIPT>
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 239
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Lost by tried and tried

  #12  
Jul 23rd, 2008
There seems to be a problem with the smallest bit I have to enter the follwing numbers in this order 89;44;20 a
This outputs 44 as the smallest if I change the order 20;44;89 then it outputs 20.
Are you sure about that ? I don't think so. It works fine for me.
And most importantly all the outputs have to be one under the other I tried adding <br> and <p> tags but this dose not work.
Try "\n".
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var firstnumber = prompt("Enter first number and Click OK", "0");
  5. var secondnumber = prompt("Enter second number and Click Ok", "0");
  6. var thirdnumber = prompt("Enter third number and Click Ok", "0");
  7. var total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
  8. var average= (Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber))/3;
  9. var largest = Math.max(firstnumber, secondnumber, thirdnumber);
  10. var smallest = Math.min(firstnumber, secondnumber, thirdnumber);
  11. alert("Sum: " + total + "\n Average: " + average + "\n smallest:" + smallest + "\n largest: " + largest +"\n")
  12. </script>
  13. </head>
  14. <body>
  15. </body>
  16. </html>
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Apr 2008
Posts: 12
Reputation: stan.joe1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stan.joe1 stan.joe1 is offline Offline
Newbie Poster

Re: Lost by tried and tried

  #13  
Jul 23rd, 2008
Originally Posted by nav33n View Post
Are you sure about that ? I don't think so. It works fine for me.

Try "\n".
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var firstnumber = prompt("Enter first number and Click OK", "0");
  5. var secondnumber = prompt("Enter second number and Click Ok", "0");
  6. var thirdnumber = prompt("Enter third number and Click Ok", "0");
  7. var total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
  8. var average= (Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber))/3;
  9. var largest = Math.max(firstnumber, secondnumber, thirdnumber);
  10. var smallest = Math.min(firstnumber, secondnumber, thirdnumber);
  11. alert("Sum: " + total + "\n Average: " + average + "\n smallest:" + smallest + "\n largest: " + largest +"\n")
  12. </script>
  13. </head>
  14. <body>
  15. </body>
  16. </html>



Check this out when I run the script on ie5 it outputs 44 as the smallest but when I run it on fire fox the output is correct!

Sadly I can not add the print screen on the post as when I click the image link it ask for a url
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 239
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Lost by tried and tried

  #14  
Jul 23rd, 2008
I tested with IE6 and FF3 and I don't see any difference in the way they work ! I don't have IE5, so I can't really tell where its going wrong(?!?)
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Jun 2008
Posts: 67
Reputation: Troy III is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
Troy III's Avatar
Troy III Troy III is offline Offline
Junior Poster in Training

Re: Lost by tried and tried

  #15  
Jul 23rd, 2008
Originally Posted by stan.joe1 View Post
Check this out when I run the script on ie5 it outputs 44 as the smallest but when I run it on fire fox the output is correct!

Sadly I can not add the print screen on the post as when I click the image link it ask for a url



That's impossible, becasuse "Math.min" and "Math.max" are implemented in javascript version 1.0 and the IE5 uses, at least JS v1.1 engine.

You have introduced some other error in your code or input.
alert(Math.min(120,845,12,8744,1,10,0,-278)) will return -278 (correct since it's the smallest number in the sequence) in all of the browsers supporting this object, otherwise you'll get an error, but never 0, nor 1 or 10 or some other wrong value!

Why don't you send us the code you are currently using to see if we can reproduce your wrong result?
Last edited by Troy III : Jul 23rd, 2008 at 3:34 pm.
Reply With Quote  
Join Date: Apr 2008
Posts: 12
Reputation: stan.joe1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stan.joe1 stan.joe1 is offline Offline
Newbie Poster

Re: Lost by tried and tried

  #16  
Jul 24th, 2008
Originally Posted by Troy III View Post
That's impossible, becasuse "Math.min" and "Math.max" are implemented in javascript version 1.0 and the IE5 uses, at least JS v1.1 engine.

You have introduced some other error in your code or input.
alert(Math.min(120,845,12,8744,1,10,0,-278)) will return -278 (correct since it's the smallest number in the sequence) in all of the browsers supporting this object, otherwise you'll get an error, but never 0, nor 1 or 10 or some other wrong value!

Why don't you send us the code you are currently using to see if we can reproduce your wrong result?


Hi there,

My completed code appears below, is there a way for me to submit a screen dump so you can see what I am talking about?

<html>
<head>
<script type="text/javascript">
var firstnumber = prompt("Enter first number and Click OK", "0");
var secondnumber = prompt("Enter second number and Click Ok", "0");
var thirdnumber = prompt("Enter third number and Click Ok", "0");
var total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
var average= (Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber))/3;
var product =Math.floor(firstnumber)* Math.floor(secondnumber)* Math.floor(thirdnumber);
var largest = Math.max(firstnumber, secondnumber, thirdnumber);
var smallest = Math.min(firstnumber, secondnumber, thirdnumber);
alert("Sum: " + total + "\n Average: " + average +"\n Product: " + product + "\n largest: " + largest +"\n smallest:" + smallest +"\n")
</script>
</head>
<body>
</body>
</html>
Reply With Quote  
Join Date: Jun 2008
Posts: 67
Reputation: Troy III is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
Troy III's Avatar
Troy III Troy III is offline Offline
Junior Poster in Training

Re: Lost by tried and tried

  #17  
Jul 24th, 2008
Originally Posted by stan.joe1 View Post
Hi there,

My completed code appears below, is there a way for me to submit a screen dump so you can see what I am talking about?

<html>
<head>
<script type="text/javascript">
var firstnumber = prompt("Enter first number and Click OK", "0");
var secondnumber = prompt("Enter second number and Click Ok", "0");
var thirdnumber = prompt("Enter third number and Click Ok", "0");
var total = Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber);
var average= (Math.floor(firstnumber)+ Math.floor(secondnumber)+ Math.floor(thirdnumber))/3;
var product =Math.floor(firstnumber)* Math.floor(secondnumber)* Math.floor(thirdnumber);
var largest = Math.max(firstnumber, secondnumber, thirdnumber);
var smallest = Math.min(firstnumber, secondnumber, thirdnumber);
alert("Sum: " + total + "\n Average: " + average +"\n Product: " + product + "\n largest: " + largest +"\n smallest:" + smallest +"\n")
</script>
</head>
<body>
</body>
</html>


Yes there is, -there are plenty of image file hosters on the web, where you can upload your image for free [but please save it in a gif or jpg format, -don't link your reply with bmp or tif images if possible ].

Your current code should be working fine by the way.

I will soon post my code.
Reply With Quote  
Join Date: Jun 2008
Posts: 67
Reputation: Troy III is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
Troy III's Avatar
Troy III Troy III is offline Offline
Junior Poster in Training

Re: Lost by tried and tried

  #18  
Jul 25th, 2008
Well, now that you've learned that quotes can be used only in the logic of parenthesis (*) , but not (*] same as "*' etc, and that strings can be added with the + sign and automatically converted into stings when you ad some var value to it.

I also think that you've learned something that 99% of very old javascript coders don't know about Math.min and Math.max and similar, that is - you are not restricted to Math.min(x,y) or Math.max(x,y) only, as scripting reference examples describe it, since there is Math.min(x,y,z,...) .

Please, don't take what you read in books so literally.
Practice! And don't forget that JavaScript is very powerful language...

One thing I don't understand, is why is there no Average Math Object, in Math object collection. Something like Math.average(45,42,47,41,46) , that will return the average numeric value of the group, but that's not so interesting for discussion.

Therefore I've already made one, and introduced it in this example as a Troy III Script to JavaScript Math.object Extension, so that others who might need that functionality may use it also.

So here it is - your last working code rewritten:

<html>
<head>
<title>Implementing Math.average Extension</TITLE>

<script src="Troy III MathAverage.js" type="text/javascript"></script>

<script type="text/javascript">

var stNumber = parseFloat(prompt("Enter 1st number and Click OK", 0));
var ndNumber = parseFloat(prompt("Enter 2nd number and Click OK", 0));
var rdNumber = parseFloat(prompt("Enter 3rd number and Click OK", 0));

var total    = stNumber + ndNumber +  rdNumber;
var smallest = Math.min( stNumber, ndNumber, rdNumber );
var average  = Math.average( stNumber, ndNumber, rdNumber );
var largest  = Math.max( stNumber, ndNumber, rdNumber );
var product  = stNumber * ndNumber * rdNumber;

//rounding to 2 digit floating precision manually:

	total   = Math.round(total*100)/100;
	smallest= Math.round(smallest*100)/100;
	average = Math.round(average*100)/100;
	largest = Math.round(largest*100)/100;
	product = Math.round(product*100)/100;

alert(	   "Smallest: " + smallest+
	 "\n Average: "  + average +
	  "\n Largest: "  + largest +
	   "\n Sum: "      + total   + 
	    "\n Product: "  + product  )
</script>
</head>
<body>
<script type="text/javascript">
//Testing my Math.average extension with a really long sequence of numbers:

alert(	"The average number of the test sequence is: "+
	Math.average(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31)
	)

</script>
</body>
</html> 

The Math.average extension file attached below[418bytes].
My kind Regards 2all.
Attached Files
File Type: js Troy III MathAverage.js (418 Bytes, 1 views)
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 8:26 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC