【发布时间】:2021-03-06 23:09:00
【问题描述】:
帮助我试图让我的代码发出警报 最重要的数字输入和最低的为什么我的不这样做?
let price;
let items = 0;
let sum=0;
let big = 0 ;
let small = 0;
price = Number(prompt("Please enter the price of the Item you choose to buy or enter -1 to stop: "));
while (price != -1){
sum=sum+price;
items++;
avg = sum/items;
Big = Math.max(Number(price));
small = Math.min(Number(price));
price = Number(prompt("Please enter the price of the Item you choose to buy or enter -1 to stop: "));
}
alert("You have purchased "+items+" items!");
alert("the sum of items you purchased is: $"+sum);
alert("the Averege of the items is: " + avg);
alert("The highest item price purchased was: "+ Big);
alert("The lowest item price was: "+ small);
【问题讨论】:
-
你必须给
Math.max()多个参数。它返回所有参数中最高的。 -
如果你只给它一个参数,它只会返回那个,因为没有其他东西可以比较。
-
你为什么写
Number(price)?您在分配变量时将其转换为数字,您不必再次转换它。 -
如何获得最高的输入回报
-
比较当前输入和
Big。如果更高,请替换Big。
标签: javascript math methods while-loop