gpt4 book ai didi

javascript - 查找最高变量并显示

转载 作者:行者123 更新时间:2023-12-02 14:25:50 24 4
gpt4 key购买 nike

我想知道如何在不使用 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>

最佳答案

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

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;
}
});

关于javascript - 查找最高变量并显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38256751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com