[英]How do I return nothing instead of a blank space in this if statement?
You can see I use "" to make the if statement return nothing in certain cases, but in reality it returns a blank space.您可以看到我使用 "" 使 if 语句在某些情况下不返回任何内容,但实际上它返回一个空格。 I don't want that white space, what could I use instead?
我不想要那个空白,我可以用什么来代替?
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();
}
You will have to restructure you code and move the include the space when the condition is truthy.当条件为真时,您将不得不重组您的代码并移动包含空间。
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}`;
To return nothing, you'd use return;
要不返回任何内容,您可以使用
return;
However, if one case returns a string, you should probably always return a string for consistency.但是,如果一种情况返回一个字符串,您可能应该始终返回一个字符串以保持一致性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.