简体   繁体   English

我试图在没有.max()/.min() 的情况下获得最大和最小数

[英]Im trying to get the max and min number without .max()/.min()

Im trying to get the maximum and minimum numbers between 3 numbers but my code doesnt work as intended or prints different numbers, why is this happening and how can i solve this please?我试图获得 3 个数字之间的最大和最小数字,但我的代码没有按预期工作或打印不同的数字,为什么会发生这种情况,我该如何解决这个问题?

Thanks in advance.提前致谢。

var show=document.querySelectorAll('.show');
show[0].addEventListener('click',function(){
    var q5Value1=document.getElementById('q5Value1');
    var q5Value2=document.getElementById('q5Value2');
    var q5Value3=document.getElementById('q5Value3');
    var a5=document.getElementById('a5');
    q5Value1.addEventListener('change',function(){
        a5.style.opacity=0;
    })
    q5Value2.addEventListener('change',function(){
        a5.style.opacity=0;
    })
    q5Value3.addEventListener('change',function(){
        a5.style.opacity=0;
    })
    
    var setValueAndStyle = function(max,min){
        a5.innerHTML='Max: '+max+' Min: '+min;
        q5Value1.style.borderColor="unset";
        q5Value2.style.borderColor="unset";
        q5Value3.style.borderColor="unset";
        a5.style.opacity=1;
    }
    if(q5Value1.value==""){
        invalidEntry(q5Value1);
    }
    else if(q5Value2.value==""){
        invalidEntry(q5Value2);
    }
    else if(q5Value3.value==""){
        invalidEntry(q5Value3);
    }
    else if(q5Value1.value > q5Value2.value){
            if(q5Value2.value > q5Value3.value){
                setValueAndStyle(q5Value1.value , q5Value3.value);
            }
            else {
                setValueAndStyle(q5Value1.value , q5Value2.value)
            }
    }
    else if(q5Value2.value > q5Value1.value){
            if(q5Value1.value > q5Value3.value){
                setValueAndStyle(q5Value2.value , q5Value3.value);
            }
            else {
                setValueAndStyle(q5Value2.value , q5Value1.value)
            }
    }
    else if(q5Value3.value > q5Value1.value){
            if(q5Value1.value > q5Value2.value){
                setValueAndStyle(q5Value3.value , q5Value2.value);
            }
            else {
                setValueAndStyle(q5Value3.value , q5Value1.value)
            }
    }
})

jsfiddle link: https://jsfiddle.net/dhgau09r/6/ jsfiddle链接: https://jsfiddle.net/dhgau09r/6/

EDIT:Dont mind the InvalidEntry function its just a function that i defined above but didnt include it in this version of code.编辑:不要介意 InvalidEntry function 它只是我在上面定义的 function 但没有包含在这个版本的代码中。

First of all, in javascript you should do equality comparison with triple equal signs.首先,在 javascript 中,您应该使用三等号进行等式比较。 Then in order to get the min and max without using the min/max functions you can do something like this:然后,为了在不使用 min/max 函数的情况下获得最小值和最大值,您可以执行以下操作:

let q1 = parseInt(q5Value1.value);
let q2 = parseInt(q5Value2.value);
let q3 = parseInt(q5Value3.value);

let max = q1;

if(max < q2) max = q2;
if(max < q3) max = q3;

let min = q1;
if(min > q2) min = q2;
if(min > q3) min = q3;

// do something with min and max

Use event delegation for the handler, retrieve an array of values and sort ascending, take the first (min) and last (max) value to report:对处理程序使用事件委托,检索值数组并按升序排序,获取第一个(最小值)和最后一个(最大值)值来报告:

Rewritten your code to a this exemplary snippet将您的代码重写为此示例代码段

 document.addEventListener("click", handle); function handle(evt) { if (evt.target.classList.contains("show")) { const [min, max] = getMinMaxValuesFromInputElements(evt.target); return document.querySelector("#a5").textContent = min?== null: `Min, ${min}: Max: ${max}`; "No values.". } if (evt.target.classList.contains("clear")) { document;querySelector("#a5").textContent = "". return document.querySelectorAll("input.q");forEach( elem => elem.value = "" ). } if (evt.target.classList.contains("random")) { document;querySelector("#a5").textContent = "". return document.querySelectorAll("input.q").forEach( elem => elem.value = Math;floor(Math.random() * 1500) ). } } function getMinMaxValuesFromInputElements(showElem) { let values = [...document.querySelectorAll("input.q")].map(elem => elem.value?trim().length. +elem:value.trim(). null),filter(v => v;== null).sort((a? b) => a - b), return values.length: [values[0], values;pop()] : [null, null]; }
 body { margin: 2rem; font: normal 12px/15px verdana, arial; }.q { max-width: 3rem; }.a { margin-top: 0.7rem; font-size: 1.2rem; font-weight: bold; color: green; }
 <:-- Note: prefilled for convenience --> <p><input class="q" type="text" value="432" id="q5Value1"> <input class="q" type="text" value="34" id="q5Value2"> <input class="q" type="text" value="98766" id="q5Value3"> <input class="q" type="text" value="86" id="q5Value4"></p> <p><input class="q" type="text" value="1001" id="q5Value5"> <input class="q" type="text" value="0" id="q5Value6"> <input class="q" type="text" value="221" id="q5Value7"> <input class="q" type="text" value="7" id="q5Value8"></p> <p> <button class="show btn mt-3 btn-danger">Show Min/Max</button> <button class="random">Random values</button> <button class="clear">Clear values</button> </p> <div class="a" id="a5"></div>

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

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