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
ἔρως
ἔρως5mo 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
BlackwolfOP5mo ago
yes, i tried returning 3 names, but how do i "call" them?
ἔρως
ἔρως5mo ago
are they functions?
Blackwolf
BlackwolfOP5mo 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
BlackwolfOP5mo ago
ahhhhh, thank you
ἔρως
ἔρως5mo ago
or, just use the array directly instead of passing the values to 3 variables
Blackwolf
BlackwolfOP5mo ago
the array is sorted in many ways from many other functions so needs its own function or i'm repeating code
ἔρως
ἔρως5mo ago
depends only seeing the code is when someone can answer that question
Blackwolf
BlackwolfOP5mo ago
that didn't quite work, it took the third name and split it into the first 3 letters of that name
ἔρως
ἔρως5mo 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
BlackwolfOP5mo ago
calling just name1 returns a full name
ἔρως
ἔρως5mo ago
you're returning just 1 name in a string you must do this too
Blackwolf
BlackwolfOP5mo ago
can i not
return name1, name2, name3
return name1, name2, name3
?
ἔρως
ἔρως5mo ago
no, javascript doesn't support multiple return values
Blackwolf
BlackwolfOP5mo ago
ahhhhh
ἔρως
ἔρως5mo ago
but you can do return [name1, name2, name3]
Blackwolf
BlackwolfOP5mo ago
and return an array of names, and break it this end ?
ἔρως
ἔρως5mo ago
what do you mean with "and break it this end"?
Blackwolf
BlackwolfOP5mo ago
i mean separate the names into individual variables
ἔρως
ἔρως5mo ago
yes, that's the only way
Blackwolf
BlackwolfOP5mo ago
oh i didn't need to - that does return 3 separate variables thank you
ἔρως
ἔρως5mo ago
you're welcome
Want results from more Discord servers?
Add your server