【问题标题】:Return variable value of JavaScript function返回 JavaScript 函数的变量值
【发布时间】:2019-10-16 15:23:54
【问题描述】:

伙计们,我有一个 JavaScript 函数,它应该返回一个带有值的数组。这是函数:

let finalInput;
let input;
function getCorrectInput (input, finalInput) {
  input = prompt("Type the client type followed by" +
  "semicollon and the dates separated by comma"+
  "(Following the example: Reward: 31Dez2019(weekday), 01Jan2020(weekday)):");

  while (input === undefined || input === null || input === '') {
    input = prompt("Type the client type followed by" +
    "semicollon and the dates separated by comma"+
    "(Following the example: Reward: 31Dez2019(weekday), 01Jan2020(weekday)):");
  } 

  if (input) 
    return 

    finalInput = input.split(',');
  
  return finalInput
}

getCorrectInput()

基本上,该函数验证字段是否包含某些内容(如果用户键入)并返回输入中的内容。所以函数的输出应该是这样的:

finalInput = ["Reward", "date1", "date2"]

我需要访问位置 [0] 处的 finalInput,并将其传递给一个变量,如下所示:

const clientType = finalInput[0]

但是当我尝试访问 finalInput 时,它返回一个错误,指出它无法读取未定义的属性 0。所以这意味着代码没有返回带有值的数组。

我做错了什么?

【问题讨论】:

  • promptwhile 循环中是一个糟糕的选择;我很想将它从问题中完全删除,因为你不能“取消”它。也就是说,if (input) return... 如果input 是任何真实值,这将退出函数。
  • 我看到return 没有分号,这是一个基本问题。我想知道,我需要进一步阅读吗?请注意,return 后跟换行符会解析为 return;
  • 条件if (input)是没有意义的,在你循环之后这个条件总是truereturn退出函数,返回undefined
  • semicollon 可能想检查您的拼写

标签: javascript arrays function


【解决方案1】:

您有 2 个名为 finalInput 的变量。

  • 在第 1 行定义的,直到 finalInput[0] 才接触(所以它是未定义的)
  • 第 3 行定义为函数参数的那个

你用return finalInput返回第二个,但你从来没有在任何地方分配它(getCorrectInput()之前什么都没有)。

  1. 删除第二个的声明,因为您从不将其作为参数传入
  2. 在函数内使用let 定义局部变量
  3. 在某处分配返回值

这样的:

function getCorrectInput () {
    let finalInput;
    let input;

    // etc

    return finalInput;
}

let finalInput = getCorrectInput();

也删除这个:

  if (input) 
    return

因为它没有明显的原因提前退出函数。

【讨论】:

    【解决方案2】:

    您正在返回之前您需要的逻辑。所以:

    if (input) 
        return // CHANGE THIS
    

    改成:

    if (input) {
        return input.split(',');
    }
    

    现在它只返回一个undefined 值,因为有输入。如果你想要一个条件来检查是否没有值,那么你需要做if (input === null || input === undefined) { // LOGIC }来检查你是否没有得到输入。

    【讨论】:

      【解决方案3】:

      你有这个:

       if (input) 
          return 
      

      所以如果input 存在,那么您将返回 undefined,因为您不返回任何内容,并且您永远不会到达返回 finalInput 的部分。

      【讨论】:

      • 应该是if (!input)
      【解决方案4】:

      这里的问题是您对输入变量的检查,当有用户输入时,表达式始终为真,并且它给出了一个无法访问的语句的结果,

      因此,要解决此问题,您需要更改对“输入”变量的检查,如下所示:

      • 如果(输入==空)

      你没事(y)

      let finalInput;
      let input;
      function getCorrectInput (input, finalInput) {
        input = prompt("Type the client type followed by" +
        "semicollon and the dates separated by comma"+
        "(Following the example: Reward: 31Dez2019(weekday), 01Jan2020(weekday)):");
      
        while (input === undefined || input === null || input === '') {
          input = prompt("Type the client type followed by" +
          "semicollon and the dates separated by comma"+
          "(Following the example: Reward: 31Dez2019(weekday), 01Jan2020(weekday)):");
        } 
      
        if (input == null ) 
          return 
      
          finalInput = input.split(',');
        
        return finalInput
      }
      //Just to log your result
      var result = getCorrectInput()
      console.log(result )

      【讨论】:

        猜你喜欢
        • 2016-11-25
        • 1970-01-01
        • 1970-01-01
        • 2012-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-22
        • 1970-01-01
        相关资源
        最近更新 更多