簡體   English   中英

如何在此 if 語句中不返回任何內容而不是空格?

[英]How do I return nothing instead of a blank space in this if statement?

您可以看到我使用 "" 使 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();
    }

當條件為真時,您將不得不重組您的代碼並移動包含空間。

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;

但是,如果一種情況返回一個字符串,您可能應該始終返回一個字符串以保持一致性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM