gpt4 book ai didi

Javascript:条件比较的麻烦 – 胡闹

转载 作者:行者123 更新时间:2023-11-28 02:04:39 25 4
gpt4 key购买 nike

有时这段代码可以正常工作,但这取决于我将要执行的部分放在哪里,这实际上是说它根本不起作用 - 但我真的不知道为什么不这样做。也许你们中的一个人能够提供一些见解:

function displayBanner(currentDate) {
var munf = currentDate.getMonth();

var imageSrc = "defaultLogo.gif";

imageSrc = (munf == 9) ? ("fallLogo.gif") : ("defaultLogo.gif");
imageSrc = (munf == 8) ? ("fallLogo.gif") : ("defaultLogo.gif");
imageSrc = (munf == 10) ? ("fallLogo.gif") : ("defaultLogo.gif");

imageSrc = (munf == 11) ? ("winterLogo.gif") : ("defaultLogo.gif");
imageSrc = (munf == 0) ? ("winterLogo.gif") : ("defaultLogo.gif");
imageSrc = (munf == 1) ? ("winterLogo.gif") : ("defaultLogo.gif");

imageSrc = (munf == 2) ? ("springLogo.gif") : ("defaultLogo.gif");
imageSrc = (munf == 3) ? ("springLogo.gif") : ("defaultLogo.gif");
imageSrc = (munf == 4) ? ("springLogo.gif") : ("defaultLogo.gif");

imageSrc = (munf == 7) ? ("summerLogo.gif") : ("defaultLogo.gif");
imageSrc = (munf == 5) ? ("summerLogo.gif") : ("defaultLogo.gif");
imageSrc = (munf == 6) ? ("summerLogo.gif") : ("defaultLogo.gif");

return imageSrc;
}

最佳答案

你有逻辑问题。所发生的情况是,即使找到了正确的月份,所有比较都会发生。你应该尝试 switch...case 方法:

switch(numf) {
case 9:
case 8:
case 10:
imageSrc = 'fallLogo.gif';
break;
case 11:
case 0:
case 1:
imageSrc = 'winterLogo.gif';
break;
case 2:
case 3:
case 4:
imageSrc = 'springLogo.gif';
break;
case 7:
case 5:
case 6:
imageSrc = 'summerLogo.gif';
break;
default:
imageSrc = 'defaultLogo.gif';
}

关于Javascript:条件比较的麻烦 – 胡闹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17950706/

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