3
u/DoNotBotherMeplz Dec 31 '22
How does one make the code look that pretty?
9
2
u/troy57890 Dec 31 '22
I'll add this to my next project before the semester. Happy New Years šš
1
2
2
2
u/TheJohnnyStein Jan 01 '23
is there a way to return a default message after the first click??
1
u/Low_Mammoth_9371 Jan 03 '23
You might want to look into setting a default parameter for the function for that.
4
u/Umesh-K Jan 01 '23
is there a way to return a default message after the first click??
One way of achieving this is
let count = 0 btn.addEventListener("click", () => { if (count == 0) { alert("Happy New Year 2023!") count++ } else alert("Have a great year ahead!") })
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?
35
u/senocular Dec 31 '22
Yes, quite a few things, including:
capture
once
passive
signal
Read more about it here: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
5
2
u/dotpr Jan 01 '23
What is options object?
3
u/albedoa Jan 01 '23
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.
26
u/AnozerFreakInTheMall Dec 31 '22
It throws "DepressionError: NY mood not found" for me.
4
3
u/spectre1006 Dec 31 '22
Gonna try this year to stay focused and finish my codecademy course for once
2
u/Moopboop207 Dec 31 '22
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.
1
2
2
5
Dec 31 '22
User won't see it though...
4
u/mattlag Dec 31 '22
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.10
Dec 31 '22
[removed] ā view removed comment
-3
Dec 31 '22
The code makes no sense and it is just karma farming imho. Sorry.
4
Dec 31 '22
[removed] ā view removed comment
4
u/iDuuck Jan 01 '23
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
1
-2
Dec 31 '22
that's really cool, i didn't know about the once option. but i do disagree with the naming. be verbose, use button or even newYearsButton.
3
u/albedoa Dec 31 '22
That's this issue you have with this?
-2
Dec 31 '22
yeah i think it sets a bad example. did i miss something else?
4
u/IAmSteven Dec 31 '22
btn is very commonly used to mean button. Like in Tailwind https://v1.tailwindcss.com/components/buttons
-1
Dec 31 '22
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.2
u/dotpr Jan 01 '23
As a beginner I can totally relate to this. Was a bit confused at
btnat first but what really gave me a hard time arectx, variables namedstring, variables named after an array function, functions namedfunctionand so on.-4
-7
-5
u/CevicheCabbage Jan 01 '23
Did you jslint this? Use double quotes instead of single quotes and function() instead of () =>
2
u/TorbenKoehn Jan 01 '23
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)
-2
Jan 01 '23
[removed] ā view removed comment
4
u/TorbenKoehn Jan 01 '23 edited Jan 01 '23
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:
Where is the string being returned and how is it being displayed? I think it should have been
alertinstead ofreturn.When the second
btn.click()runs, nothing happensābecause of{once: true}āand consequently nothing returns, not evenundefined.