简体   繁体   English

返回 JavaScript function 的变量值

[英]Return variable value of JavaScript function

So guys, I have a JavaScript function that should return an Array with values.所以伙计们,我有一个 JavaScript function 应该返回一个带有值的数组。 Here is the function:这是 function:

 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()

Basically, the function verifies if the field contains something (if the user typed) and returns the content inside the input.基本上,function 验证字段是否包含某些内容(如果用户键入)并返回输入中的内容。 So the output of the function should be something like that:所以 function 的 output 应该是这样的:

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

I need to access the finalInput at position [0], and pass it to a variable, like that:我需要访问 position [0] 处的 finalInput,并将其传递给变量,如下所示:

const clientType = finalInput[0]

But when I try to access the finalInput, it returns an error saying that it cannot read property 0 of undefined.但是当我尝试访问 finalInput 时,它返回一个错误,指出它无法读取未定义的属性 0。 So it means that the code is not returning the array with the values.所以这意味着代码没有返回带有值的数组。

What am I doing wrong?我究竟做错了什么?

You have 2 variables named finalInput .您有 2 个名为finalInput的变量。

  • The one defined on line 1 which you don't touch until finalInput[0] (so it is undefined)在第 1 行定义的那个,直到finalInput[0]才触摸(所以它是未定义的)
  • The one defined as a function parameter on line 3第 3 行定义为 function 参数的那个

You return the second one with return finalInput but you never assign it anywhere (there is nothing before getCorrectInput() ).您使用return finalInput返回第二个,但您从未将其分配到任何地方(在getCorrectInput()之前没有任何内容)。

  1. Remove the declaration of the second one, since you never pass it in as an argument删除第二个的声明,因为您从不将其作为参数传入
  2. Define your local variables with let inside the function在 function 中使用let定义局部变量
  3. Assign the return value somewhere在某处分配返回值

Such:这样的:

function getCorrectInput () {
    let finalInput;
    let input;

    // etc

    return finalInput;
}

let finalInput = getCorrectInput();

Also remove this:还要删除这个:

 if (input) return

As it exits the function early for no apparent reason.由于它没有明显的原因提前退出 function。

You are returning before the logic you need.你在你需要的逻辑之前返回。 So:所以:

if (input) 
    return // CHANGE THIS

Change it to:将其更改为:

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

Right now it just returns an undefined value because there is input.现在它只返回一个undefined的值,因为有输入。 If you want a conditional to check if there is NOT a value, then you need to do if (input === null || input === undefined) { // LOGIC } to check if you didn't get input.如果你想要一个条件来检查是否没有值,那么你需要做if (input === null || input === undefined) { // LOGIC }来检查你是否没有得到输入。

You have there this:你有这个:

 if (input) 
    return 

so if input exists then you return undefined as you do not return anything and you never reach the part where you return the finalInput.因此,如果input存在,那么您将返回 undefined,因为您不返回任何内容,并且您永远不会到达返回 finalInput 的部分。

The problem here is your check on the input variable, the expression is always true while there's a user entry, and it gives result to an unreachable statements,这里的问题是您对输入变量的检查,当有用户输入时,表达式始终为真,并且它为无法访问的语句提供结果,

So to solve this you need to change the check on the 'input' variable as follow:因此,要解决此问题,您需要更改对“输入”变量的检查,如下所示:

  • if( input == null )如果(输入== null)

And you are OK (y)你没事(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 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM