gpt4 book ai didi

javascript - 使用 .includes() 时 Switch 语句不起作用

转载 作者:行者123 更新时间:2023-11-30 15:22:58 26 4
gpt4 key购买 nike

client.on('chat', function(channel, userstate, message, self){



switch(message){
case message.includes(emotes[0]):
numberOfEmotes[0]++;
console.log("Emote0 has been used " + numberOfEmotes[0] + " time(s)");
break;
case message.includes(emotes[1]):
numberOfEmotes[1]++;
console.log("Emote1 has been used " + numberOfEmotes[1] + " time(s)");
break;
case message.includes(emotes[2]):
numberOfEmotes[2]++;
console.log("Emote2 has been used " + numberOfEmotes[2] + " time(s)");
break;
case message.includes(emotes[3]):
numberOfEmotes[3]++;
console.log("Emote3 has been used " + numberOfEmotes[3] + " time(s)");
break;
}

/* if(message.includes(emotes[0])){
numberOfEmotes[0]++;
console.log("Emote0 has been used " + numberOfEmotes[0] + " time(s)");
}*/


//console.log("** " + message + " **");
});

当函数 chat.on 被调用时,带有字符串的消息变量应该通过 switch 语句运行,我有一个包含不同字符串的数组,如果消息包含来自该数组的字符串,则运行该案例。但是什么也没发生,一切似乎都是对的,哪里出了问题?

最佳答案

解决此问题的一种方法是使用 switch(true) 而不是 switch(method)。这将起作用,因为它将 true(boolean) 与 case 语句进行比较。包含 emote[0] 的 Case 语句将返回 true,因此它将继续执行该 block 。希望对您有所帮助。

关于javascript - 使用 .includes() 时 Switch 语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43423028/

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