简体   繁体   English

查找最高变量并显示

[英]Finding highest variable and displaying

I would like to know how to find the highest variable without using math.max. 我想知道如何在不使用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> 

Let's store all the days in an array: 让我们将所有日期存储在一个数组中:

var days = [ parseInt(monday), parseInt(tuesday), parseInt(wednesday), parseInt(thursday), parseInt(friday), parseInt(saturday), parseInt(sunday) ];

You can find then find the max value by first establishing a base max value (0). 您可以先建立基本max (0),然后找到最大值。 You then iterate over each day, and if it's value is more than the max value, assign this value to max : 然后,您每天都要进行迭代,如果它的值大于max ,则将此值分配给max

var max = 0;
days.forEach(function(day) {
    if(day.value > max) {
        max = day.value;
    }
});

you can do: 你可以做:

function max() {
   return Math.max(Number(monday.value) + Number(tuesday.value) + Number(wednesday.value) + Number(thursday.value) + Number(friday.value) + Number(saturday.value) + Number(sunday.value));
}

This is a VERY RUDIMENTARY APPROACH of doing this. 这是一个非常基本的方法 It's good to read through and understand what's going on, then eventually put all of the hours into an array. 最好通读并了解正在发生的事情,然后最终将所有时间都放在一个数组中。

If you're doing beginner's homework like someone else mentioned, this is probably as far as you've gotten 如果您像其他人一样在做初学者的功课,这可能是您所掌握的

function getHighest() {
    var hrs_monday = Number(monday.value)
    var hrs_tuesday = Number(tuesday.value)
    var hrs_wednesday = Number(wednesday.value)
    var hrs_thursday = Number(thursday.value)
    var hrs_friday = Number(friday.value)
    var hrs_saturday = Number(saturday.value)
    var hrs_sunday = Number(sunday.value);
    var most_hrs = 0;

    if(hrs_monday > most_hrs) {
        most_hrs = hrs_monday;
    } 
    if(hrs_tuesday > most_hrs) {
        most_hrs = hrs_tuesday;
    } 
    if(hrs_wednesday > most_hrs) {
        most_hrs = hrs_wednesday;
    }    
    if(hrs_thursday > most_hrs) {
        most_hrs = hrs_thursday;
    } 
    if(hrs_friday > most_hrs) {
        most_hrs = hrs_friday;
    } 
    if(hrs_saturday > most_hrs) {
        most_hrs = hrs_saturday;
    } 
    if(hrs_sunday > most_hrs) {
        most_hrs = hrs_sunday;
    } 

    alert("Most hours " + most_hrs);
}

A better way of doing it is to put all of your variables into an array, then iterate through the array and overwrite the most_hrs variable: 更好的方法是将所有变量放入数组,然后遍历数组并覆盖most_hrs变量:

var hrs_array = [];

hrs_array.push(Number(monday.value));
hrs_array.push(Number(tuesday.value));
hrs_array.push(Number(wednesday.value));
hrs_array.push(Number(thursday.value));
hrs_array.push(Number(friday.value));
hrs_array.push(Number(saturday.value));
hrs_array.push(Number(sunday.value));

max_hrs = 0;

//Now you have all of your numbers in an array
//do this when you get the numbers, not necessarily all together

for(var i = 0; i < hrs_array.length; i++){
    if(hrs_array[i] > max_hrs) {
        max_hrs = hrs_array[i];
    }
}

alert("Maximum Hours = " + max_hrs);

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. 制作一个max变量,该变量将存储一天“游戏”的最大小时数。

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 . 如果前一个索引的值小于当前索引的值,则将当前索引分配给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. 这是为您工作的jsfiddle

To meet the requirements of the exercise you could write your own version of Math.max, say numericMax and use it as you would Math.max (for you do devise). 为了满足练习的要求,您可以编写自己的Math.max版本,说出numericMax并像Math.max一样使用它(可自行设计)。

You will need to familiarize yourself with the arguments object , global properties Infinity , NaN , possibly the global function Number and, of course, Math.max for a desription of what it does. 您需要熟悉参数object ,全局属性InfinityNaN ,可能还需要全局函数Number ,当然还需要Math.max ,以了解其功能。

So the following example shows how numericMax might be written and is intended as a reference if you have difficulty writing and getting your own version to work (if you choose this approach, of course). 因此,以下示例显示了如何编写numericMax并在您编写和使自己的版本无法正常工作时(当然,如果选择此方法)将作为参考。

function numericMax(){
    if( arguments.length == 0){
        return -Infinity;
    }
    for( var i = 0; i < arguments.length; ++i){
        if(typeof arguments[i] != "number"){
             arguments[i] = Number(arguments[i]);
        }
    }
    var max = arguments[0];
    if(isNaN(max))
        return NaN;
    for( i = 1; i < arguments.length; ++i){
        num = arguments[i];
        if( isNaN(num)){
            return NaN;
        }
        if( num > max){
            max = num;
        }
    }
    return max;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM