如何显示星期几(我有最大值)
How can I display the day of the week, (I have the highest value)?
我将如何显示日期,我在一周中有最高值,但我需要将最大值与一周中的正确日期相匹配。如果您有想法,请使用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)
相关文章:
- 在Javascript数组中查找绝对最大值
- Html5输入属性的默认值,如最小值、最大值、大小等
- 按最大值获取数组索引
- 识别滑块范围的最小值和最大值
- 如何在最大值之前的某个点停止范围滑块
- 访问Highcharts系列的最小/最大值:afterSetExtremes不会激发
- 如何在拖动后获得图表的最小值和最大值放大高图表
- 如何仅在存在最小值和最大值时才显示错误消息
- 当达到最小值和最大值时,防止在输入中显示额外的文本
- 显示最大值的控制编号
- 如果 kendo 数字文本框中的值增加最大值,则希望显示错误消息
- Jquery 最小值和最大值显示新页面
- 是否可以在剑道UI线性仪表上仅显示最大值/最小值
- Highcharts:在箱线图中显示标签(最小值,最大值,中位数等)
- 动态更改标签以显示y轴最小值和最大值
- 为什么评级总是显示评级的最大值,而隐藏字段则不然
- y轴最小和最大值范围显示
- 当最后一个点在当前x轴最小值之前,下一个点在当前x轴最大值之后时,高图不显示线
- 使用C3JS仅在Y轴上显示最小/最大值
- 如何显示星期几(我有最大值)