javascript,输入的最大打印数量有问题

时间:2018-10-11 04:13:41

标签: javascript

如果输入0,则要求程序终止,否则它将继续运行。如果输入0,则需要在控制台上打印输入的最大值。 需要一些帮助修复此代码。它并不总是打印输入的最大数字。有时候会。

     var number = 1;
     var largest = 0;

 do {
       number = prompt("Enter a number other than 0 and will tell you the largest");
       if(number >= largest) {    //If largest is small, set current number 
       as largest
       largest = number;
       }
    }  while (number > 0);
       console.log("The largest number is " + largest);

1 个答案:

答案 0 :(得分:0)

您必须将prompt的值转换为数字。

var number = 1;
var largest = 0;

do {

  number = prompt("Enter a number other than 0 and will tell you the largest");

  if (parseFloat(number) >= largest)
    largest = number;
  
  } while (number > 0);

console.log("The largest number is " + largest);