How to fetch data from a function?

This works, but I need to elaborate on it ...
name1 = getname(names)
name1 = getname(names)
I am sending a list of names to a function that sorts them in various ways and retrieving the name at the top of the list. The function has
return name1
return name1
I am stuck on the syntax for retrieving 3 names ... name1, name2 and name3
23 Replies
ἔρως
ἔρως4mo ago
assuming a sorted array, you can do this: array.slice(0, 3) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice there's also the horrible return [name[0], name[1], name[2]] option
Blackwolf
BlackwolfOP4mo ago
yes, i tried returning 3 names, but how do i "call" them?
ἔρως
ἔρως4mo ago
are they functions?
Blackwolf
BlackwolfOP4mo ago
name1, name2, name3 = getname(names)
name1, name2, name3 = getname(names)
doesn't work i just want to put 3 names into 3 variables
Blackwolf
BlackwolfOP4mo ago
ahhhhh, thank you
ἔρως
ἔρως4mo ago
or, just use the array directly instead of passing the values to 3 variables
Blackwolf
BlackwolfOP4mo ago
the array is sorted in many ways from many other functions so needs its own function or i'm repeating code
ἔρως
ἔρως4mo ago
depends only seeing the code is when someone can answer that question
Blackwolf
BlackwolfOP4mo ago
that didn't quite work, it took the third name and split it into the first 3 letters of that name
ἔρως
ἔρως4mo ago
that's because you're returning a string, not the elements of the array but it did work otherwise, each variable wouldn't have the letters of the first name
Blackwolf
BlackwolfOP4mo ago
calling just name1 returns a full name
ἔρως
ἔρως4mo ago
you're returning just 1 name in a string you must do this too
Blackwolf
BlackwolfOP4mo ago
can i not
return name1, name2, name3
return name1, name2, name3
?
ἔρως
ἔρως4mo ago
no, javascript doesn't support multiple return values
Blackwolf
BlackwolfOP4mo ago
ahhhhh
ἔρως
ἔρως4mo ago
but you can do return [name1, name2, name3]
Blackwolf
BlackwolfOP4mo ago
and return an array of names, and break it this end ?
ἔρως
ἔρως4mo ago
what do you mean with "and break it this end"?
Blackwolf
BlackwolfOP4mo ago
i mean separate the names into individual variables
ἔρως
ἔρως4mo ago
yes, that's the only way
Blackwolf
BlackwolfOP4mo ago
oh i didn't need to - that does return 3 separate variables thank you
ἔρως
ἔρως4mo ago
you're welcome
Want results from more Discord servers?
Add your server