James L James L - 4 months ago 9
HTML Question

Finding highest variable and displaying

I would like to know how to find the highest variable without using math.max. My aim of my code is to ask the user questions their gaming. The user will enter their gaming hours each day and then it will total all hours, average them and then display most amount of gaming on such days. I am new to programming, any help is good!

Here is my code so far...



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>






Answer

Assign all the days in an array.

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

Make a max variable that will store the maximum value of hours that one "gamed" in one day.

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

Loop through the array to find the biggest value. If a the previous index has a smaller value than the current index, assign current index to max.

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

Here is a working jsfiddle for you.

Comments