如何使用JavaScript For Loop添加提示框?

时间:2017-02-15 15:08:02

标签: javascript loops for-loop

我做了一个while循环,它允许汇总提示框并显示最大数字。我试图将While循环转换为For循环。我遇到麻烦,因为我对For循环几乎一无所知,任何帮助都会受到赞赏。感谢



var a = Number(prompt("Enter First Number"));
var b = Number(prompt("Enter Second Number"));
var c = Number(prompt("Enter Third Number"));

while (a > 0) {
  if (a > b && a > c) {
    alert(prompt("A is Bigger"));
  }
  if (b > a && b > c) {
    alert(prompt("B is Bigger"));
  }
  if (c > a && c > b) {
    alert(prompt("C is Bigger"));
  } else {
    alert(prompt("Each number is the same"))
  }
  alert(prompt(a + b + c));
}




1 个答案:

答案 0 :(得分:0)

这是另一种方法,条件较少,没有任何不必要的循环。希望你喜欢它= D

var a = Number(prompt("Enter First Number"));
var b = Number(prompt("Enter Second Number"));
var c = Number(prompt("Enter Third Number"));

var biggerNumber = Math.max(a, b, c);
if(a == b && b == c){
  alert("Each number is the same.");
} else {
  var indexBiggerNumber = [a,b,c].indexOf(biggerNumber) + 1;
  alert(`The ${indexBiggerNumber}º number is bigger.`);
}
alert(a + b + c);