◆ JavaScript

Get length of an object

This is real work, not a feature someone invented — it comes from real job ads and real questions people asked. Below are four ready AI prompts: get it done, make it easy for the next person to say yes to, work out the right move when you are stuck, and stop it coming back.

4prompts

The same task, four prompts

today's deadline · the next reviewer · the stuck moment · the pattern
AExecute — do the immediate taskGet the number of items in the 'shoppingCart' array for the user's current…+
Get the number of items in the 'shoppingCart' array for the user's current session.
when the reply comes backPush once: ask it to sharpen the weakest part, and to say what it assumed. Helpful?
BImprove — make it easier to acceptWhen the user adds an item, display the updated item count for their 'shoppingCart' in the…+
When the user adds an item, display the updated item count for their 'shoppingCart' in the navigation bar, and if it's zero, show 'Your cart is empty' instead of '0 items'.
when the reply comes backPush once: ask it to sharpen the weakest part, and to say what it assumed. Helpful?
CDecide — diagnose the stuck momentThe shopping cart display on the checkout page is inconsistent. Sometimes it shows 'undefined…+
The checkout page is showing 'undefined items' when the cart is empty, but '0 items' when it's just been cleared.
The shopping cart display on the checkout page is inconsistent. Sometimes it shows 'undefined items', other times '0 items', and I'm not sure why. I'm worried this looks broken to the customer and might lead to abandoned carts. What's the best way to reliably get the length of an object or array, ensuring it always returns a number, even if the object is null or undefined, so the display is consistent?
when the reply comes backPush once: ask it to sharpen the weakest part, and to say what it assumed. Helpful?
DBecome — change the patternI often run into issues where I'm trying to get the length of a list or object, and it turns…+
I frequently struggle with getting the correct length of arrays or objects, especially when they might be null or undefined.
I often run into issues where I'm trying to get the length of a list or object, and it turns out to be null or undefined, causing runtime errors. This forces me to add defensive checks everywhere, slowing me down. What's a better habit or pattern I can adopt to consistently and safely determine the size of collections, regardless of their initial state?
when the reply comes backPush once: ask it to sharpen the weakest part, and to say what it assumed. Helpful?

Questions people actually ask

honest answers, no sign-up

Every task here was seen in the real world. Someone doing the job named it, a real job ad asked for it, or a lot of people asked about it online.

If nothing real showed a task, it is not on the page. That is the whole rule.

They are the same job approached four ways, because what you need depends on where you are.

Get it done today. Make it easy for the next person to say yes to. Work out the right move when you are stuck. Learn the pattern so the job stops coming back.

For most of these jobs it can carry the heavy thinking - draft it, sort it, check it, rehearse it with you.

It cannot sit in your chair, take the blame when a number is wrong, or notice what nobody wrote down. Let it do the first 80%. Keep the last 20% that is truly yours.

No. Copy any prompt and paste it into the AI you already use. No account, no score, no wall in the way.

Any of them. The prompts describe the work rather than naming a product, so they are not tied to one assistant.

That is also why they keep working when you switch.

Change it freely. Every prompt is a starting line, not a rule.

Put in your real numbers, your real names and your real deadline. The more you make it yours, the better the answer comes back.

The tasks come from real job ads, published job data and the questions people ask in public forums.

The steps come from JavaScript's own documentation, with practitioner sources for the traps the manual does not mention.

Push once. Ask it to sharpen the weakest part and to say what it assumed.

Most wrong answers come from a missing detail rather than a bad prompt - tell it the thing it could not know.