[英]How can I shorten this?
我正在尋找縮短我目前正在從事的項目的硬編寫代碼。 問題是我不知道該怎么做,因為我還很新。
我已經硬編碼,用谷歌搜索我還能做什么,但沒有任何幫助。
if (spins >= 3 && spins <= 5) {
textSize(40);
text("H", 20, 40);
} if (spins >= 6 && spins <= 8) {
textSize(40);
text("HA", 20, 40);
} if (spins >= 9 && spins <= 11) {
textSize(40);
text("HAP", 20, 40);
} if (spins >= 12 && spins <= 14) {
textSize(40);
text("HAPP", 20, 40);
} if (spins >= 15 && spins <= 17) {
textSize(40);
text("HAPPY", 20, 40);
}
沒有任何問題我只是想縮短它,它按原樣完美運行,但我仍在努力學習,我領先於 class,但我無法從谷歌或我的同行那里找到幫助。
請注意,文本字符串長度直接對應於Math.floor(spins / 3)
- 使用它來確定要傳遞給text
的字符串長度:
const strLen = Math.floor(spins / 3);
textSize(40);
text('HAPPY'.slice(0, strLen), 20, 40);
const getText = (spins) => { const strLen = Math.floor(spins / 3); console.log('HAPPY'.slice(0, strLen)); }; getText(3); getText(4); getText(5); getText(6); getText(14); getText(15);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.