简体   繁体   English

如何求两个数的平均值?

[英]How to get the average of two numbers?

var thing1 = prompt("what is a number")
var thing2 = prompt("what is a number")

alert(Math.min(thing1,thing2));

I am able to get the minimum of the two members.我能够得到两个成员中的最小值。
How do I change this to give the average of the two numbers asked?我如何更改它以给出所询问的两个数字的平均值?

Here's a function that gets an average:这是一个获取平均值的函数:

function average(){
  var a = arguments, l = a.length;
  if(l){
    for(var i=0,n=0; i<l; i++){
      n += a[i];
    }
    return n/l;
  }
  return false;
}
// horrible idea to use prompt and alert
var prompt1 = prompt('enter a number');
var prompt2 = prompt('enter another number');
alert(average(prompt1, prompt2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM