如何显示星期几(我有最大值)

How can I display the day of the week, (I have the highest value)?

本文关键字:最大值 显示 何显示      更新时间:2023-09-26

我将如何显示日期,我在一周中有最高值,但我需要将最大值与一周中的正确日期相匹配。如果您有想法,请使用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>
<!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>

<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>

试试这个

var d = new Date();
var n = d.getDay();

你应该创建一个星期几的数组,如下所示:

var days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']

然后在名为"arr"的数组中找到 indexOf max 数字,以查找星期几作为数字(从星期一开始)(当然,这是在代码中找到最大值之后)

var dayNumber = arr.indexOf(max);

然后通过 do 找到与该数字对应的星期几

var day = days[dayNumber]

希望这有帮助!

另外:找到最大值的更简单方法是使用内置的 Math.max 函数,如下所示

Math.max.apply(null, arr)