【问题标题】:Recursive prompt function returns null递归提示函数返回null
【发布时间】:2019-05-30 01:52:57
【问题描述】:

我想构建一个提示函数,它会一直提示直到用户输入一个值,然后返回该值。

为什么当我在强制模式下进入然后输入一个值时,这段代码返回null?任何人都可以让它工作吗?

function UserInput(text, defaultText, mandantory) {
  if (typeof defaultText === 'undefined')
    defaultText = '';
  if (typeof mandantory === 'undefined')
    return prompt(text, defaultText);
  else {
    var a = prompt(text, defaultText);
    if (a === '') {
      return UserInput(text, defaultText, mandantory);
    } else {
      return null;   
    }
  }
}
<!DOCTYPE html>
<html>
	<head>
		<title>Page Title</title>
	</head>
	<body>
		<button onclick="alert(UserInput('prompt with input', ''))">prompt with input</button><br/>
		<button onclick="alert(UserInput('prompt with mandantory input', '', 0))">prompt with mandantory input</button>
	</body>
</html>

注意:必须从 onclick="..." 调用它。

谢谢, 德让

【问题讨论】:

  • Else 返回 a 而不是在 else 中返回 null。

标签: javascript variables if-statement prompt


【解决方案1】:

它返回null,因为你打电话给你做return null,如果a'',你必须返回a

function UserInput(text, defaultText, mandantory) {
  if (typeof defaultText === 'undefined')
    defaultText = '';
  if (typeof mandantory === 'undefined')
    return prompt(text, defaultText);
  else {
    var a = prompt(text, defaultText);
    if (a === '') {
      return UserInput(text, defaultText, mandantory);
    } else {
      return a;
    }
  }
}
<!DOCTYPE html>
<html>

<head>
  <title>Page Title</title>
</head>

<body>
  <button onclick="alert(UserInput('prompt with input', ''))">prompt with input</button><br/>
  <button onclick="alert(UserInput('prompt with mandantory input', '', 0))">prompt with mandantory input</button>
</body>

</html>

但我会在这里使用 while do while 循环而不是递归:

function UserInput(text, defaultText, mandantory) {
  if (typeof defaultText === 'undefined')
    defaultText = '';

  var a

  do {
    // the first prompt will always be called
    a = prompt(text, defaultText)
    // repeat the loop while  a === '' and mandantory !== 'undefined'
  } while (mandantory !== 'undefined' && a === '')

  return a;
}
<!DOCTYPE html>
<html>

<head>
  <title>Page Title</title>
</head>

<body>
  <button onclick="alert(UserInput('prompt with input', ''))">prompt with input</button><br/>
  <button onclick="alert(UserInput('prompt with mandantory input', '', 0))">prompt with mandantory input</button>
</body>

</html>

【讨论】:

    【解决方案2】:

    它返回null,因为如果输入了值,您将在else 中返回它。在您最后的else 中,当a'' 不同时,您需要返回a 而不是null

    if (a === '') {
      return UserInput(text, defaultText, mandantory);
    } else {
      return a;   
    }
    

    注意:

    要检查是否定义了变量,您可以使用if(mandatory) 而不是写if(typeof mandantory === 'undefined')

    演示:

    function UserInput(text, defaultText, mandantory) {
      if (typeof defaultText === 'undefined')
        defaultText = '';
      if (mandantory)
        return prompt(text, defaultText);
      else {
        var a = prompt(text, defaultText);
        if (a === '') {
          return UserInput(text, defaultText, mandantory);
        } else {
          return a;   
        }
      }
    }
    <!DOCTYPE html>
    <html>
    	<head>
    		<title>Page Title</title>
    	</head>
    	<body>
    		<button onclick="alert(UserInput('prompt with input', ''))">prompt with input</button><br/>
    		<button onclick="alert(UserInput('prompt with mandantory input', '', 0))">prompt with mandantory input</button>
    	</body>
    </html>

    【讨论】:

      【解决方案3】:

      在你的 sn-p 中:

      1. 如果a 不为零,则始终返回null 而它应该返回return a
      2. 如果有return语句,则不需要else分支
      3. 您可以使用默认参数
      4. 强制(错字)=> n
      5. 应避免返回相同值的多个分支

      你可以编写如下函数:

      const UserInput = async (text, defaultText = '', mandatory = false) => {
        const result = await prompt(text, defaultText);
        
        if (!result && mandatory) {
          console.log('User did not enter a correct value, try again');
          return UserInput(text, defaultText, mandatory);
        }
        
        console.log(`Returning Value: "${result}"`);
        return result;
      };
      
      document
        .getElementById('test')
        .addEventListener('click', () => UserInput('Say Something', '', true))
      ;
      &lt;button id="test"&gt;Try&lt;/button&gt;

      【讨论】:

        【解决方案4】:

        你可以这样做

        function UserInput(text, defaultText, mandantory) {
          if (typeof defaultText === 'undefined')
            defaultText = '';
          if (typeof mandantory === 'undefined')
            return prompt(text, defaultText);
          else {
            var a = prompt(text, defaultText);
            if (a === '') {
              return UserInput(text, defaultText, mandantory);
            } else if (a !== null) {
              return a;   
            } else {
              return null;   
            }
          }
        }
        <!DOCTYPE html>
        <html>
        	<head>
        		<title>Page Title</title>
        	</head>
        	<body>
        		<button onclick="alert(UserInput('prompt with input', ''))">prompt with input</button><br/>
        		<button onclick="alert(UserInput('prompt with mandantory input', '', 0))">prompt with mandantory input</button>
        	</body>
        </html>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-06-29
          • 1970-01-01
          • 2022-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多