/    Sign up×
Community /Pin to ProfileBookmark

Just learning/relearning JS

Anyone…

Where do I go to show my code and output result to get guidance on what I’m doing wrong?

In my case, my code example works, but consistently shows ‘undefined’ as the last output result.

Please advise.

J

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@sibertJan 09.2022 — Post your code here as a start. Preferable within codetags. Or use Codepen or https://jsfiddle.net
Copy linkTweet thisAlerts:
@jlgehauthorJan 09.2022 — @sibert#1641286
<i>
</i>const veggies = ['broccoli', 'spinach', 'cauliflower', 'broccoflower'];

const politelyDecline = (veg) =&gt; {
console.log(<span><code>No ${veg} please. I will have pizza with extra cheese.</code></span>);
}

const declineEverything = (arr) =&gt; {
return console.log(arr.forEach(politelyDecline));
}

declineEverything(veggies);

const grudginglyAccept = (arr) =&gt; {
return console.log(<span><code>Ok, I guess I will eat some ${arr}.</code></span>);
}

const acceptEverything = (arr) =&gt; {
return console.log(arr.forEach(grudginglyAccept));
}

acceptEverything(veggies);


My results show four expected outputs, but each set ends with a fifth result as 'undefined'. I honestly don't see why it is doing that. Can you spot it and let me know? I will try that website you included as well.

Thanks.

Jeff
Copy linkTweet thisAlerts:
@jlgehauthorJan 09.2022 — I tried the 'fiddle' website and got the same results. I also spotted a bad practice(not critical) by not including a 'return' with my first 'const' declaration. I added it, but still got the same result. I wasn't expecting any real changes, but figured I would try it anyway.

Jeff
Copy linkTweet thisAlerts:
@daveyerwinJan 09.2022 — ``<i>
</i>const veggies = ['broccoli', 'spinach', 'cauliflower', 'broccoflower'];

const politelyDecline = (veg) =&gt; {
console.log(
No ${veg} please. I will have pizza with extra cheese.);
return 1;
}

const declineEverything = (arr) =&gt; {

arr.forEach(politelyDecline);
return 1;
}

declineEverything(veggies);

const grudginglyAccept = (arr) =&gt; {
console.log(
Ok, I guess I will eat some ${arr}.);
return 1;
}

const acceptEverything = (arr) =&gt; {
arr.forEach(grudginglyAccept);
return 1;
}

acceptEverything(veggies);<i>
</i>
``
Copy linkTweet thisAlerts:
@jlgehauthorJan 10.2022 — @DaveyErwin#1641310

Thank you, but why did I receive the 'undefined' result?

Jeff
Copy linkTweet thisAlerts:
@daveyerwinJan 10.2022 — > @jlgeh#1641313asked ... why did I receive the 'undefined' result?

because arr.forEach(politelyDecline) returns undefined

so does arr.forEach(grudginglyAccept)
Copy linkTweet thisAlerts:
@jlgehauthorJan 11.2022 — But WHY did it return 'undefined'? It should have only given me four resultant outputs, but yet it gave me five. Why?

That's what I can't seem to figure out or understand.

Jeff
Copy linkTweet thisAlerts:
@SempervivumJan 11.2022 — @jlgeh#1641374

@DaveyErwin explained correctly:
``<i>
</i>const politelyDecline = (veg) =&gt; {
// This line outputs "No xxx please. I will have pizza with extra cheese."
// four times as required:
console.log(
No ${veg} please. I will have pizza with extra cheese.);
}

const declineEverything = (arr) =&gt; {
// After looping through the array, this line outputs "undefined"
// as the return value of arr.forEach is undefined:
return console.log(arr.forEach(politelyDecline));
}<i>
</i>
``
×

Success!

Help @jlgeh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...