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?
You have 2 variables named finalInput
.
finalInput[0]
(so it is undefined)You return the second one with return finalInput
but you never assign it anywhere (there is nothing before getCorrectInput()
).
let
inside the functionSuch:
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.
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. 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.
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.
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:
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 )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.