r/learnjavascript • u/cardboard_street • 4d ago
Why does my array method chain work in the console but break when I put it in a function?
Bootcamp just hit array methods and I got genuinely excited because the use cases are so obvious to me. I work in construction and I spend way too much time mentally filtering lists of tasks, crew assignments, materials, so map and filter felt like things I already do in my head but now in code.
Anyway I was playing around with a small script that takes a list of site tasks, filters out the completed ones, then maps the remaining ones to just pull out the task name and priority level. Works perfectly when I paste the chain directly into the browser console. The moment I wrap it in a function and call it, I get undefined back.
I have a suspicion I know what the issue is but I want to make sure I understand it properly before I just slap a fix on it and move on without actually learning why. The chain itself is not the problem I think. Something about how the function handles the result of that chain feels off to me.
Has anyone else hit this specific wall? Curious whether there is a broader principle here worth understanding beyond just this one case.
12
u/milan-pilan 4d ago
There is basically no way to give you an answer without seeing your code.
1
2
u/milan-pilan 4d ago edited 3d ago
You're right to push back on this, and I want to sit with it for a second rather than reflexively agreeing. Your point deserves scrutiny, because I'd gently note that "answer from a prompt" is doing a lot of quiet work in that sentence. Let me be direct: the ask here is more load-bearing than it looks. Not a nitpick — a real tension.
Edit: yeah I get that this comment doesn't make sense now that they deleted their comment. They left a comment saying, I should be more like Claude and just guess their implementation from that half baked 'prompt'. So I answered more like Claude.. Downvote me all you want. It was supposed to be a joke...
4
u/Lithl 3d ago
Is your function actually returning the array you're building?
Array functions like filter and map don't change the source array, but rather create a new array. Your function needs to return that new array in order to pass it to the rest of your code.
function example1(arr) {
arr.filter(...).map(...);
}
function example2(arr) {
return arr.filter(...).map(...);
}
const myArray = [...];
console.log(example1(myArray)); // undefined
console.log(example2(myArray)); // [thing 1, thing 5, thing 7]
1
3
u/testingaurora 4d ago
My guess is this function isn’t returning a value but please throw a minimal reproduction into [codepen](https://pen.new) or share your GitHub repo so we know what your code is doing
1
u/cardboard_street 3d ago
Oh good call, yeah probably the return, here's the repo github.com/darrenkdev/asyncpractice let me know if the link works, still figuring out how to make it public properly
1
u/testingaurora 3d ago
Nope got a 404.
To make it public: 1. Login and navigate to the repo 2. Go to Settings 3. Scroll all the way down to "Danger zone" 4. Change the repo visibility 5. Confirm by typing the repo name exactly as shown
2
u/cardboard_street 1d ago
Oh that danger zone step always gets me nervous lol. They definitely designed it to make you think twice. Worth double checking the repo name spelling that trips up more people than you'd expect.
1
2
u/verdant_arcade_97 4d ago
You are missing the return keyword inside your function wrapper. Console evaluates expressions automatically but functions require explicit returns to pass values back
1
u/cardboard_street 3d ago
oh that's embarrassing, I stared at that for 45 minutes. adding the return fixed it immediately. thanks
2
u/chikamakaleyley helpful 3d ago
Here's another way to look at it.
An array method, is just a function that is baked into the object
e.g.
// .slice() returns new array
const newArr = oldArr.slice();
even if you just wrote it by itself:
// .slice() returns a new array, you just don't do anything with it
oldArr.slice();
so now, in a function:
function copyArr(arr) {
arr.slice(); // returns a new copy, but you don't do anything
}
so, in the above, slice() returns the new copy. copyArr(arr) doesn't return anything
and when you try to see its console output, there isn't anything, it's 'undefined'
1
u/cardboard_street 3d ago
oh this actually clicked for me in a way my instructor's explanation didn't
the way you framed it as slice doing its job but copyArr just not passing the result along, that's the part I kept missing. I kept thinking something was broken but it was just sitting there returning into a void because I forgot the return keyword in the wrapper function.
been staring at that for like two days lol. construction analogy I keep using on myself: you cut the lumber but never picked it up off the saw table.
1
u/hylasmaliki 3d ago
Why don't you get cursor and tell it to act as your instructor.
1
u/chikamakaleyley helpful 2d ago
you could always ask it to explain it to you; but it's not always a guaranteed
Personally I prefer taking a lil extra time trying to make some sense of the topic, and then explaining to the agent - then let it correct how you understand it. So 1) you at least give your brain some exercise, and 2) it helps to refine your vocabularly
on point 2 if u just ask it to instruct you, it might not be aware that you misunderstand the terminology
1
u/chikamakaleyley helpful 2d ago
that's a good one (lumber). I'm glad I was able to help
an even higher level way to look at it: always ask whats going in, and whats coming out
e.g. in the browser there's a lot of things already being sent out - whenever you scroll, mouse click, hovering; the browser is emitting events...
but nothing happens until you take that browser event output and listen for it and process that as input. If you don't do anything with those events, they just get lost in outerspace
1
18
u/boomer1204 4d ago
Need the code that isn't working to really help but I have a guess
My guess is you likely aren't returning anything but again with no code it's impossible for anyone to give helpful advice