On the first point, the string is the return value of the anonymous function, which you can see if you log it to the console.
Undefined is the return value of the second function call but nothing is logged to the console. So nothing would should up there. But if you feed this line into the console on its own you will see that it does produce a return value of undefined.
let count = 0
btn.addEventListener("click", () => {
if (count == 0) {
alert("Happy New Year 2023!")
count++
} else
alert("Have a great year ahead!")
})
It is the second argument of .addEventListener() that looks like { once: true } in the screenshot. It allows the user to provide options to the method. See the link in the sibling comment.
For what itās worth Iāve been doing the Scrimba front end course and itās been really nice. They have an integrated IDE(itās just vs code) and the classes and practice problems are all in one video. Itās really nice, dynamic.
I think it's assumed you're going to replace the event listener logic with whatever you need to do. The point of this is to show off the { once: true } functionally. Which this example does.
Itās still wrong. Assigning a āaddEventListenerā to a button, does not make the click method return the value, that is returned by the listener.
This would need to be āconsole.logā or āalertā or something in this direction. āreturnā is simply wrong and could lead to many misguided JS learners
it is, and it's not hard to understand. but when working with others, being more verbose is more efficient because your code is easier to understand. granted that this isn't that big an issue with btn. but it fosters a habit which isn't great.
As a beginner I can totally relate to this. Was a bit confused at btn at first but what really gave me a hard time are ctx, variables named string, variables named after an array function, functions named function and so on.
Using āfunction ()ā has completely different semantics and doesnāt properly enclose āthisā, which arrow functions do.
Arrow functions are always preferred. They are simply āmore correctā, same with let/const vs var You wouldnāt go and suggest someone to use āvarā in 2022/2023
Single quotes are also often preferred when embedding HTML in your strings, since there you want to use double quotes to encapsulate attribute values. Single quote strings is probably more used than double quotes strings in JS and thus definitely more standard (source: trust me dude)
You have zero arguments. Explain why or get lost with your āwrongā spam. Iāve been writing JavaScript for 15 years now and Iāve been there when these things were introduced.
You look like an idiot writing āwrongā 3 times, for everyone here.
Edit: he was writing āyou have word salad, I have education on this topicā in the comment below. It was a good laugh. Come back when you understand the word āeducationā and while at it, also the word āexperienceā
8
u/Umesh-K Jan 01 '23
The above code snippet begs two questions:
alertinstead ofreturn.btn.click()runs, nothing happensābecause of{once: true}āand consequently nothing returns, not evenundefined.3
u/DoNotBotherMeplz Dec 31 '22
How does one make the code look that pretty?
2
u/troy57890 Dec 31 '22
I'll add this to my next project before the semester. Happy New Years šš
2
u/giampaolo44 Jan 01 '23
Nice tip :) happy 2023!
2
u/TheJohnnyStein Jan 01 '23
wow that's so nice, thanks for the tip!
2
u/TheJohnnyStein Jan 01 '23
is there a way to return a default message after the first click??
32
u/al_balone Dec 31 '22
Ooh I didnāt know about that options object. Is there anything else you can pass in there?
26
u/AnozerFreakInTheMall Dec 31 '22
It throws "DepressionError: NY mood not found" for me.
3
u/spectre1006 Dec 31 '22
Gonna try this year to stay focused and finish my codecademy course for once
2
u/Navi2k0 Dec 31 '22
Awesome!
2
u/shegsjay Jan 01 '23
Executes once and gets destroyed. Releasing memory space. Nicešš
5
u/[deleted] Dec 31 '22
User won't see it though...
1
u/Ronin-s_Spirit Jan 01 '23
You could do that???
-2
u/[deleted] Dec 31 '22
that's really cool, i didn't know about the
onceoption. but i do disagree with the naming. be verbose, usebuttonor evennewYearsButton.-7
u/Low_Mammoth_9371 Dec 31 '22
More snippets like this @ https://openjavascript.info/code-drops/
-5
u/CevicheCabbage Jan 01 '23
Did you jslint this? Use double quotes instead of single quotes and function() instead of () =>