返回JavaScript函数的变量值 [英] Return variable value of JavaScript function
问题描述
伙计们,我有一个JavaScript函数,该函数应返回带有值的Array.功能如下:
let finalInput;让输入;函数getCorrectInput(输入,finalInput){输入=提示(键入客户端类型,后跟" +"semicollon和用逗号分隔的日期" +(以下为示例:奖励:31Dez2019(工作日),2020年1月1日(工作日):");while(输入===未定义||输入===空||输入===)){输入=提示(键入客户端类型,后跟" +"semicollon和用逗号分隔的日期" +(以下为示例:奖励:31Dez2019(工作日),2020年1月1日(工作日):");}如果(输入)返回finalInput = input.split(',');返回finalInput}getCorrectInput()
基本上,该函数验证字段是否包含某些内容(如果用户键入了内容),并在输入中返回内容.因此,该函数的输出应如下所示:
finalInput = ["Reward","date1","date2"]
我需要访问位置[0]的finalInput,并将其传递给变量,如下所示:
const clientType = finalInput [0]
但是当我尝试访问finalInput时,它返回一个错误,表明它无法读取未定义的属性0.因此,这意味着代码不会返回带有值的数组.
我做错了什么?
这里的问题是您对输入变量的检查,当有用户输入时,表达式始终为true,并且将结果赋予不可达的语句,>
因此,要解决此问题,您需要更改对"input"变量的检查,如下所示:
- if(input == null)
您还可以(y)
let finalInput;让输入;函数getCorrectInput(输入,finalInput){输入=提示(键入客户端类型,后跟" +"semicollon和用逗号分隔的日期" +(以下为示例:奖励:31Dez2019(工作日),2020年1月1日(工作日):");while(输入===未定义||输入===空||输入===)){输入=提示(键入客户端类型,后跟" +"semicollon和用逗号分隔的日期" +(以下为示例:奖励:31Dez2019(工作日),2020年1月1日(工作日):");}如果(输入== null)返回finalInput = input.split(',');返回finalInput}//只需记录您的结果var结果= getCorrectInput()console.log(result)
So guys, I have a JavaScript function that should return an Array with values. Here is the 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. So the output of the function should be something like that:
finalInput = ["Reward", "date1", "date2"]
I need to access the finalInput at position [0], and pass it to a variable, like that:
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. So it means that the code is not returning the array with the values.
What am I doing wrong?
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 )
And you are OK (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 )
这篇关于返回JavaScript函数的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!