简体   繁体   English

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

[英]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.

相关问题 我该如何解决:“渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,什么也不渲染,返回 null。” - How can I solve: “Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.” 如何返回此对象,而不是记录它? - How do I return this object, instead of logging it? 如何在这些 if 语句中使用 return 语句? - How do I use return statement in these if statements? 有return语句并且什么都不返回 - have return statement and return nothing 如果查询为空,如何将 Algolia 设置为不返回任何结果? - How do I set Algolia to not return any results if the query is blank? 如果未在字段中输入任何内容,如何获得返回“ 0”而不是NaN的结果。 - How can I get the result to return '0' instead of NaN if nothing is entered into the field. 在Javascript中,如果if语句不返回任何内容,则什么也不做 - In Javascript, if the if statement don't return anything, then do nothing observable - 如何使用 forEach 和 setTimeout 返回值? (与 Rxjs 无关) - observable - how do I return values using forEach and setTimeout? (nothing to do with Rxjs) return 语句在 javascript 中不返回任何内容 - return statement returns nothing in javascript 在递归中不返回任何内容的 return 语句 - return statement that returns nothing in recursion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM