gpt4 book ai didi

javascript - 如何检查一个变量是否大于多个其他变量?

转载 作者:行者123 更新时间:2023-11-30 12:01:28 24 4
gpt4 key购买 nike

我需要检查一个变量是否大于 4 个其他变量。有没有办法做到这一点?到目前为止我已经尝试过了

if(Guitar > Percussion || Brass || Keyboard || Woodwind)
{
var guitarpage = document.getElementById("GuitarPage");
guitarpage.style.display ="block";
}

if(Percussion > Guitar || Brass || Keyboard || Woodwind)
{
var percussionpage = document.getElementById("PercussionPage");
percussionpage.style.display ="block";

}

if(Brass > Percussion || Guitar || Keyboard || Woodwind) {
var brasspage = document.getElementById("BrassPage");
brasspage.style.display ="block";
}

if(Keyboard > Percussion || Guitar || Brass || Woodwind) {
var keyboardpage = document.getElementById("KeyboardPage");
keyboardpage.style.display ="block";
}

if(Woodwind > Percussion || Guitar || Keyboard || Brass) {
var woodpage = document.getElementById("WoodwindPage");
woodpage.style.display ="block";
}

以及使用 && 但我要么显示每个 div,要么根本不显示。

最佳答案

Guitar > Percussion || Brass || Keyboard || Woodwind

评估为

(Guitar > Percussion) || Brass || Keyboard || Woodwind

如果 Guitar > Percussion 为真,或者 BrassKeyboardPercussion 中的第一个假值>.

你想要的是(Guitar > Percussion && Guitar > Brass && Guitar > Keyboard && Guitar > Woodwind)。更好的是,您可以这样做:

var vals = [Guitar, Percussion, Brass, Keyboard, Woodwind];
var pageNames = ["GuitarPage", "PercussionPage", "BrassPage", "KeyboardPage", "WoodwindPage"];
var max = Math.max.apply(null, vals);
var index = vals.indexOf(max);
if (vals.indexOf(max, index + 1) == -1) {
var pageName = pageNames[index];
var page = document.getElementById(pageName);
page.style.display = "block";
}

编辑:if 检查是否有第二个值也是 max,并避免在这种情况下进行操作;这使得它等同于 OP 的代码。

关于javascript - 如何检查一个变量是否大于多个其他变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36539661/

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