寻找最高变量并显示

时间:2016-07-07 23:21:16

标签: javascript html loops

我想知道如何在不使用math.max的情况下找到最高的变量。我的代码的目的是向用户询问他们的游戏问题。用户将每天输入他们的游戏时间,然后它将累计所有小时,平均他们,然后在这些天显示大部分游戏。 我是编程新手,任何帮助都很好!

到目前为止,这是我的代码......



function total() {
  var th = Number(monday.value) + Number(tuesday.value) + Number(wednesday.value) + Number(thursday.value) + Number(friday.value) + Number(saturday.value) + Number(sunday.value);
  alert("You gamed for " + th + " hours this week");



  var ah = th / 7;
  alert("Your average is " + ah + " hours this week");
}



button.onclick = total;

\

<!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="draft.css">
<link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico" />
<title>The Gaming Hours Quiz</title>

<body>
  <h1>The Gaming Hours Quiz</h1>
</body>
<p>Welcome to the Gaming Hours Quiz. Please fill out the neccesary information correctly to get your true results</p>

<h3 id="nametitle">What is your name?</h3>
<input id="name" type="letter" name="" value="type name here...">

<h3>How many hours have you gamed on Monday?</h3>
<input id="monday" type="number" name="" value="0">


<h3>How many hours have you gamed on Tuesday?</h3>
<input id="tuesday" type="number" name="" value="0">


<h3>How many hours have you gamed on Wednesday?</h3>
<input id="wednesday" type="number" name="" value="0">


<h3>How many hours have you gamed on Thursday?</h3>
<input id="thursday" type="number" name="" value="0">


<h3>How many hours have you gamed on Friday?</h3>
<input id="friday" type="number" name="" value="0">


<h3>How many hours have you gamed on Saturday?</h3>
<input id="saturday" type="number" name="" value="0">


<h3>How many hours have you gamed on Sunday?</h3>
<input id="sunday" type="number" name="" value="0">
<br>
<br>
<button id="button">Submit</button>
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:2)

让我们将所有日子存储在一个数组中:

var days = [ parseInt(monday), parseInt(tuesday), parseInt(wednesday), parseInt(thursday), parseInt(friday), parseInt(saturday), parseInt(sunday) ];

您可以通过首先建立基本max值(0)找到最大值。然后,您每天进行迭代,如果它的值超过max值,请将此值指定给max

var max = 0;
days.forEach(function(day) {
    if(day.value > max) {
        max = day.value;
    }
});

答案 1 :(得分:0)

你可以这样做:

function max() {
   return Math.max(Number(monday.value) + Number(tuesday.value) + Number(wednesday.value) + Number(thursday.value) + Number(friday.value) + Number(saturday.value) + Number(sunday.value));
}

答案 2 :(得分:0)

这是一种非常严重的方法。阅读并理解正在发生的事情是很好的,然后最终将所有时间都放入阵列中。

如果你像其他人提到的那样做初学者的家庭作业,这可能就是你已经有了

function getHighest() {
    var hrs_monday = Number(monday.value)
    var hrs_tuesday = Number(tuesday.value)
    var hrs_wednesday = Number(wednesday.value)
    var hrs_thursday = Number(thursday.value)
    var hrs_friday = Number(friday.value)
    var hrs_saturday = Number(saturday.value)
    var hrs_sunday = Number(sunday.value);
    var most_hrs = 0;

    if(hrs_monday > most_hrs) {
        most_hrs = hrs_monday;
    } 
    if(hrs_tuesday > most_hrs) {
        most_hrs = hrs_tuesday;
    } 
    if(hrs_wednesday > most_hrs) {
        most_hrs = hrs_wednesday;
    }    
    if(hrs_thursday > most_hrs) {
        most_hrs = hrs_thursday;
    } 
    if(hrs_friday > most_hrs) {
        most_hrs = hrs_friday;
    } 
    if(hrs_saturday > most_hrs) {
        most_hrs = hrs_saturday;
    } 
    if(hrs_sunday > most_hrs) {
        most_hrs = hrs_sunday;
    } 

    alert("Most hours " + most_hrs);
}

更好的方法是将所有变量放入数组中,然后遍历数组并覆盖most_hrs变量:

var hrs_array = [];

hrs_array.push(Number(monday.value));
hrs_array.push(Number(tuesday.value));
hrs_array.push(Number(wednesday.value));
hrs_array.push(Number(thursday.value));
hrs_array.push(Number(friday.value));
hrs_array.push(Number(saturday.value));
hrs_array.push(Number(sunday.value));

max_hrs = 0;

//Now you have all of your numbers in an array
//do this when you get the numbers, not necessarily all together

for(var i = 0; i < hrs_array.length; i++){
    if(hrs_array[i] > max_hrs) {
        max_hrs = hrs_array[i];
    }
}

alert("Maximum Hours = " + max_hrs);

答案 3 :(得分:0)

分配数组中的所有日期。

var arr = [Number(monday.value), Number(tuesday.value), Number(wednesday.value), Number(thursday.value), Number(friday.value), Number(saturday.value), Number(sunday.value)];

制作一个max变量,用于存储一天内“游戏”的最大小时值。

var hh = 0;
var max = arr[0];

循环遍历数组以找到最大值。如果前一个索引的值小于当前索引,则将当前索引分配给max

for(var i = 1;i < arr.length;i++) {
if(arr[i-1] < arr[i]) {
max = arr[i];
  }
}

这是一个有效的jsfiddle

答案 4 :(得分:0)

为了满足练习的要求,您可以编写自己的Math.max版本,比如numericMax,并像使用Math.max一样使用它(对于您的设计)。

您需要熟悉arguments object,全局属性InfinityNaN,可能还有全局函数Number,当然还有Math.max了解它的作用。

以下示例显示了function numericMax(){ if( arguments.length == 0){ return -Infinity; } for( var i = 0; i < arguments.length; ++i){ if(typeof arguments[i] != "number"){ arguments[i] = Number(arguments[i]); } } var max = arguments[0]; if(isNaN(max)) return NaN; for( i = 1; i < arguments.length; ++i){ num = arguments[i]; if( isNaN(num)){ return NaN; } if( num > max){ max = num; } } return max; } 的编写方式,如果您在编写和使用自己的版本时遇到困难(如果选择此方法,则可作为参考)疗程)。

{{1}}