我如何显示当天,我一周内的价值最高,但我需要将最高值与一周中正确的日期相匹配。如果您有任何想法请使用jsfiddle或其他程序来显示!谢谢!
<script>
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");
var arr = [Number(monday.value), Number(tuesday.value), Number(wednesday.value), Number(thursday.value), Number(friday.value), Number(saturday.value), Number(sunday.value)]
var hh = 0;
var max = arr[0];
for(var i = 1;i < arr.length;i++) {
if(arr[i-1] < arr[i]) {
max = arr[i];
}
}
alert("Maximum hours you have gamed in one day is " + max);
}
button.onclick = total;
</script>
&#13;
<!DOCTYPE html>
<html>
<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>
</html>
&#13;
答案 0 :(得分:0)
试试这个
var d = new Date();
var n = d.getDay();
答案 1 :(得分:0)
<script>
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");
var arr = [Number(monday.value), Number(tuesday.value), Number(wednesday.value), Number(thursday.value), Number(friday.value), Number(saturday.value), Number(sunday.value)]
var hh = 0;
var max = arr[0];
var days = ["Monday","Tuesday",...];
var dayOfMax = 0;
for(var i = 1;i < arr.length;i++) {
if(arr[i-1] < arr[i]) {
max = arr[i];
dayOfMax = i; // why not just get the index
}
}
alert("Maximum hours you have gamed in one day is " + max);
alert("The day when you have gamed the maximum is " + days[dayOfMax]);
// 0 for Monday, 1 for Tuesday, 2 for Wednesday, and so on
}
button.onclick = total;
</script>
<!DOCTYPE html>
<html>
<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>
</html>
答案 2 :(得分:0)
您应该创建一周中的日期数组,如下所示:
var days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
然后找到indexOf数组yo中的最大数字yo,称为'arr',以星期几的形式查找星期几(从星期一开始)(当然,在您的代码中找到最大值后,Thi)
var dayNumber = arr.indexOf(max);
然后通过
找到与该数字对应的星期几var day = days[dayNumber]
希望这有帮助!
另外:找到最大值的更简单方法是使用内置的Math.max函数,如此
Math.max.apply(null, arr)