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