您可以看到我使用了"quot;在某些情况下,使if语句不返回任何内容,但实际上它返回了一个空格。我不想要那个空白处,我能用什么代替呢?
if (n >= 20000 && n < 100000) {
result = `${tens[Math.floor(n / 10000) - 1]} ${
Math.floor((n % 10000) / 1000) != 0
? num[Math.floor((n % 10000) / 1000)]
: ""
} thousand ${n % 1000 != 0 ? number2words(n % 1000) : ""}`;
return result.trim();
}
当条件为true时,您将不得不重组代码并移动include空格。
const tensText = tens[Math.floor(n / 10000) - 1];
const divisible = Math.floor((n % 10000) / 1000);
const hundredsText = divisible != 0 ? ` ${num[divisible]}` : '';
const thousandsText = n % 1000 != 0 ? ` ${number2words(n % 1000)}` : '';
result = `${tensText}${hundredsText} thousand${thousandsText}`;
要不返回任何内容,您可以使用return;
但是,如果一个案例返回了一个字符串,那么为了一致性,您可能应该始终返回一个字符串。