代码之家  ›  专栏  ›  技术社区  ›  Dejan Dozet

递归提示函数返回空值

  •  2
  • Dejan Dozet  · 技术社区  · 6 年前

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

    为什么此代码返回 无效的 当我进入强制模式,然后输入一个值时?有人能让它工作吗?

    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=…”调用。

    谢谢, 德扬

    4 回复  |  直到 6 年前
        1
  •  1
  •   cнŝdk    6 年前

    它回来了 null 因为你把它放回你的 else 如果输入值。在你的最后 其他的 你得回去 a 而不是 无效的 不同于 '' :

    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>
        2
  •  1
  •   t.niese    6 年前

    它返回 null 因为你叫你做 return null 万一 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;
    }
    <!文档类型HTML>
    & HTML& GT;
    
    &头;
    <title>页面标题</title>
    和/头& GT;
    
    和身体;
    <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>
    和/身体;
    
    & lt//html & gt;
        3
  •  0
  •   Budhesh Sambad    6 年前

    你可以这样做

    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>
        4
  •  0
  •   Hitmands    6 年前

    在您的代码片段中:

    1. 如果 a 不是零,然后总是返回 null 虽然它应该 return a
    2. 不需要 else 如果有分支 return 声明
    3. 可以使用默认参数
    4. mandantory(typo)=> 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))
    ;
    <button id="test">Try</button>