SitePoint Sponsor 

User Tag List
Results 1 to 10 of 10
Thread: window alert syntax

May 20, 2007, 21:05 #1
 Join Date
 May 2007
 Posts
 22
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
window alert syntax
I am trying to display the results of and addition, average and product of three numbers in an alert window. I am able to display one of three outputs using
window.alert ( "The Sum is "+ sum + "") but I do not know how to append the other two in the box.
Also how would I write the xhtml code to find the smallest of three numbers?
thanks
cal

May 21, 2007, 04:42 #2
 Join Date
 May 2007
 Location
 Gothenburg, Sweden
 Posts
 27
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
You could use this, you can pass as many arguments(numbers) as you want:
Code:var Calc = { aNumbers : new Array(), setNumbers : function() { if(typeof arguments[0] == "object") arguments = arguments[0]; for(var i = 0; i < arguments.length; i++) { Calc.aNumbers[i] = Number(arguments[i]); } Calc.aNumbers = Calc.aNumbers.sort(Calc.sortit); }, getSum : function() { if(Calc.aNumbers.length == 0) Calc.setNumbers(arguments); var nSum = 0; for(var i = 0; i < Calc.aNumbers.length; i++) { nSum += Calc.aNumbers[i]; } return nSum; }, getProduct : function() { if(Calc.aNumbers.length == 0) Calc.setNumbers(arguments); var nProduct = 1; for(var i = 0; i < Calc.aNumbers.length; i++) { nProduct *= Calc.aNumbers[i]; } return nProduct; }, getMin : function() { if(Calc.aNumbers.length == 0) Calc.setNumbers(arguments); return(Calc.aNumbers[0]); }, getMax : function() { if(Calc.aNumbers.length == 0) Calc.setNumbers(arguments); return(Calc.aNumbers[Calc.aNumbers.length  1]); }, getAverage : function() { if(Calc.aNumbers.length == 0) Calc.setNumbers(arguments); var nSum = Calc.getSum(); return(nSum / Calc.aNumbers.length); }, sortit : function(a,b) { return(ab); } }
HTML Code:function setNumbers() { //Input field values: var nA = document.getElementById("number1").value; var nB = document.getElementById("number2").value; var nC = document.getElementById("number3").value; //Here you probably want to validate the user input values. Calc.setNumbers(nA, nB, nC); alert("The sum is " + Calc.getSum() + "\n" + "The product is " + Calc.getProduct() + "\n" + "The average is " + Calc.getAverage() + "\n" + "The lowest number is " + Calc.getMin() + "\n" + "The highest number is " + Calc.getMax()); }
HTML Code:<a href="#" onclick="alert(Calc.getSum(1,4,2,6,3)); return(false);">Sum</a><br /> <a href="#" onclick="alert(Calc.getProduct(1,4,2,6,3)); return(false);">product</a><br /> <a href="#" onclick="alert(Calc.getAverage(1,4,2,6,3)); return(false);">average</a><br /> <a href="#" onclick="alert(Calc.getMin(1,4,2,6,3)); return(false);">min</a><br /> <a href="#" onclick="alert(Calc.getMax(1,4,2,6,3)); return(false);">max</a><br />

May 21, 2007, 04:51 #3
 Join Date
 May 2007
 Posts
 28
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Try this:
Code:window.alert('The Sum of 5, 2 and 3 is ' + eval(5+2+3) + ', minimum ' + Math.min(5,2,3) + ', product ' + eval(5*2*3) + ' and average ' + eval((5+2+3)/3));
PHP Functions Class/Php Stuff: phpclassesfunctions.blogspot.com
SMS World: smstreasure.blogspot.com
Finance Business India: financebusinessindia.blogspot.com
Retail Market India: retailmarketindia.blogspot.com

May 21, 2007, 05:22 #4

May 21, 2007, 13:27 #5
 Join Date
 Nov 2001
 Location
 Bath, UK
 Posts
 2,498
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Hello, I'd like to introduce you to functional programming. It is a bit like an alternative to object oriented programming. One idea in functional programming it to try and keep functions "pure", that is, so that they don't have side effects. This means that whenever you call a function with the same set of arguments, it should return the same value.
The Calc.getSum function in the code above is not "pure". If you call it like this:
Code:alert(Calc.getSum(1,2,3));
Code:alert(Calc.getSum(100,200)); alert(Calc.getSum(1,2,3));
The behaviour of Calc.getSum changes because it has "side effects", which is another way to say that calling it changes the state of the program. If we can rewrite Calc.getSum so that it does not have any side effects, then whenever we see Calc.getSum(1,2,3), we will know that it will return 6. This makes it much easier to use, as we only need to look at one line of code to know what will be returned.
Here is one way to write such a sum function. I have called it Math.sum so that it sounds similar to Math.min and Math.max, and it can be used in the same way.
Code:Math.sum = function() { var total = 0; for (var i = 0; i < arguments.length; i++) { total += arguments[i]; } return total; };
Code:alert(Math.sum(100,200)); alert(Math.sum(1,2,3));
We can write a very similar function for the product:
Code:Math.product = function() { var total = 1; for (var i = 0; i < arguments.length; i++) { total *= arguments[i]; } return total; };
The last number we want to calculate is the average. This is just the sum divided by the number of arguments, so:
Code:Math.average = function() { return Math.sum.apply(Math, arguments) / arguments.length; };
hth,
DouglasHello World

May 21, 2007, 14:25 #6
 Join Date
 May 2007
 Location
 Gothenburg, Sweden
 Posts
 27
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
DougBTX: You are absolutely right and it was a real blunder of me .

May 21, 2007, 16:02 #7
 Join Date
 May 2007
 Posts
 22
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Thanks for the reponses, I am a beginner and was looking for the basic/novice way to get the alert box to display outputs using
window.alert ( "The Sum is "+ sum + ""). The sum displays but I do not know how to get the average, product, ect to display in the box.
THis is a script in an xhtml file.
Also how would I write the xhtml code to find the smallest of three numbers?
thanks
cal

May 21, 2007, 18:32 #8

May 21, 2007, 23:33 #9
 Join Date
 May 2007
 Posts
 22
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
I have to take three inputs from the user and display in the alert bow the largest and smallest along with the sum, product, ect

May 22, 2007, 22:46 #10
 Join Date
 May 2007
 Posts
 28
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Use below code:
Code:<script language="javascript"> var nums = window.prompt("first number, second number, third number","10,5,3"); var numbs = nums.split(","); var a = isNaN(numbs[0])?0:parseInt(numbs[0]); var b = isNaN(numbs[1])?0:parseInt(numbs[1]); var c = isNaN(numbs[2])?0:parseInt(numbs[2]); window.alert('The Sum of '+a+', '+b+' and '+c+' is ' + (a+b+c) + ', minimum ' + Math.min(a,b,c) + ', product ' + (a*b*c) + ' and average ' + ((a+b+c)/3)); </script>
PHP Functions Class/Php Stuff: phpclassesfunctions.blogspot.com
SMS World: smstreasure.blogspot.com
Finance Business India: financebusinessindia.blogspot.com
Retail Market India: retailmarketindia.blogspot.com
Bookmarks