Javascript 我怎样才能缩短这个?

Javascript 我怎样才能缩短这个?,javascript,Javascript,我正在寻找缩短我的一个项目,我目前正在工作的硬代码。问题是我不知道怎么做,因为我是新来的 我已经硬编码,谷歌搜索我还能做些什么,但什么都帮不了我 if (spins >= 3 && spins <= 5) { textSize(40); text("H", 20, 40); } if (spins >= 6 && spins <= 8) { textSize(40); text("HA", 20, 40); } if (

我正在寻找缩短我的一个项目,我目前正在工作的硬代码。问题是我不知道怎么做,因为我是新来的

我已经硬编码,谷歌搜索我还能做些什么,但什么都帮不了我

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);
}

如果(spins>=3&&spins=6&&spins=9&&spins=12&&spins=15&&spins请注意,文本字符串长度直接对应于
Math.floor(spins/3)
-使用它来确定传递给
文本的字符串长度:

const strLen = Math.floor(spins / 3);
textSize(40);
text('HAPPY'.slice(0, strLen), 20, 40);
const getText=(旋转)=>{
常数strLen=数学楼层(旋转/3);
console.log('HAPPY'.slice(0,strLen));
};
getText(3);
getText(4);
getText(5);
getText(6);
getText(14);

getText(15);
我投票将此问题作为离题题题结束,因为此问题属于on