gpt4 book ai didi

javascript - 从 switch 语句获取计数

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

在通过 switch 语句运行返回计数后,我在更改返回计数时遇到问题。我的代码如下所示:

var count = 0;
switch ($(this).last().data('card')) { // Regulating the count to be higher/lower
// Hearts
case 'h2':
return count++;
case 'h3':
return count++;
case 'h4':
return count++;
case 'h5':
return count++;
case 'h6':
return count++;
case 'h10':
return count--;
case 'hj':
return count--;
case 'hq':
return count--;
case 'hk':
return count--;
case 'ha':
return count--;

// Spades
case 's2':
return count++;
case 's3':
return count++;
case 's4':
return count++;
case 's5':
return count++;
case 's6':
return count++;
case 's10':
return count--;
case 'sj':
return count--;
case 'sq':
return count--;
case 'sk':
return count--;
case 'sa':
return count--;

// Diamonds
case 'd2':
return count++;
case 'd3':
return count++;
case 'd4':
return count++;
case 'd5':
return count++;
case 'd6':
return count++;
case 'd10':
return count--;
case 'dj':
return count--;
case 'dq':
return count--;
case 'dk':
return count--;
case 'da':
return count--;

// Clubs
case 'c2':
return count++;
case 'c3':
return count++;
case 'c4':
return count++;
case 'c5':
return count++;
case 'c6':
return count++;
case 'c10':
return count--;
case 'cj':
return count--;
case 'cq':
return count--;
case 'ck':
return count--;
case 'ca':
return count--;
}
})();

console.log(count);

当我尝试控制台将其注销时,它只是输出 0。即使我将 jQuery 替换为“h2”,它也不能解决问题。

我希望有人能帮助我:-)

最佳答案

When I'm trying to console log it out, it simply outputs 0.

你正在做后缀增量:

返回计数++;

这将返回 count 的值,然后递增 count 的值。

这也一样:

return count--;

这将返回 count 的值,然后递减 count 的值。

简单地解决这个问题:

return ++count; 

还有

return --count; 

Postfix and Prefix operators

关于javascript - 从 switch 语句获取计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42965724/

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