SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    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

  2. #2
    SitePoint Enthusiast
    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(a-b); }
      }
    And then, to alert the values, you could create a function looking like this:
    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());
      }
    Or, if you want to seperate the return values, you can call the function using this:
    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 />

  3. #3
    SitePoint Enthusiast
    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: php-classes-functions.blogspot.com
    SMS World: sms-treasure.blogspot.com
    Finance Business India: financebusinessindia.blogspot.com
    Retail Market India: retailmarketindia.blogspot.com

  4. #4
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ashishmat1979 View Post
    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));
    No need for eval, brackets by themselves will do fine (as brackets get evaluated first).

    Code:
    window.alert('The Sum of 5, 2 and 3 is ' + (5+2+3) + ', minimum ' + Math.min(5,2,3) + ', product ' + (5*2*3) + ' and average ' + ((5+2+3)/3));
    Hello World

  5. #5
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr.Flink View Post
    You could use this, you can pass as many arguments(numbers) as you want.
    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));
    Then we get an alert with "6" in it. But, if we do this:

    Code:
    alert(Calc.getSum(100,200));
    alert(Calc.getSum(1,2,3));
    Then the same call to Calc.getSum(1,2,3) returns 300 instead of 6. This means that when we call Calc.getSum(1,2,3), we could get any number back. This makes Calc.getSum hard to use, because we have to know about every previous call to Calc.getSum in the page to know what our current call to Calc.getSum will do.

    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 re-write 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;
    };
    Now, when we write this,

    Code:
    alert(Math.sum(100,200));
    alert(Math.sum(1,2,3));
    We get 300 and 6 back, as you might expect.

    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;
    };
    Note that this function is also side-effect free.

    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;
    };
    Notice that we can use the Math.sum function directly, as we don't need to worry about how it has been used previously.

    hth,
    Douglas
    Hello World

  6. #6
    SitePoint Enthusiast
    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 .

  7. #7
    SitePoint Member
    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

  8. #8
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by calamine View Post
    Also how would I write the xhtml code to find the smallest of three numbers?
    There is no XHTML code to do that, you need to use Javascript.

    The minimum of three numbers can be found using Math.min:

    Code:
    alert(Math.min(1,2,3)); // => alerts 1
    Hello World

  9. #9
    SitePoint Member
    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

  10. #10
    SitePoint Enthusiast
    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: php-classes-functions.blogspot.com
    SMS World: sms-treasure.blogspot.com
    Finance Business India: financebusinessindia.blogspot.com
    Retail Market India: retailmarketindia.blogspot.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •