Programming thread

  • 🐕 I am attempting to get the site runnning as fast as possible. If you are experiencing slow page load times, please report it.
Okay i'm almost finished this but I'm stuck on one thing.

Is there a way in vanilla Javascript to remove multiple event listeners in this case? I used a forEach method to append a button along with an eventListener to each item in an array, and want to make it so that if you click one button, eventListeners for all buttons immediately go away (i.e. all buttons are disabled).

I tried making buttonClickResponse() disable itself by either setting it equal to "null" or redefining it as an empty func, but I got nothing. Also checked multiple stackOverflows and got nothing as well. And of course, the "removeEventListener" thing at the end of buttonClickResponse() doesn't work; I'm just using it as a placeholder.

JavaScript:
            } else {
                generatedQuiz[currentQuestion].answers.forEach(addButton)
                function addButton(thisAnswer) {
                    let answerButton = document.createElement("button")
                    answerButton.textContent = thisAnswer.text
                    answerButton.style.fontFamily = "Corbel"
                    answerButton.style.padding = "1em 2em 1em 2em"
                    answerButton.className = "option"
                    answerContainer.appendChild(answerButton)
                    answerButton.addEventListener("click", buttonClickResponse)
                    function buttonClickResponse() {
                        if (thisAnswer.isCorrect) {
                            console.log("I guess you're right")
                            clearInterval(questionCountdown)
                            correctAnswerCount++
                            nextButton.addEventListener("click", showNextQuestion)
                        } else {
                            console.log("u wrong")
                            clearInterval(questionCountdown)
                            nextButton.addEventListener("click", showNextQuestion)
                        }
                        answerButton.removeEventListener("click", buttonClickResponse)
                    }
                }
            }
 
Okay i'm almost finished this but I'm stuck on one thing.

Is there a way in vanilla Javascript to remove multiple event listeners in this case? I used a forEach method to append a button along with an eventListener to each item in an array, and want to make it so that if you click one button, eventListeners for all buttons immediately go away (i.e. all buttons are disabled).

I tried making buttonClickResponse() disable itself by either setting it equal to "null" or redefining it as an empty func, but I got nothing. Also checked multiple stackOverflows and got nothing as well. And of course, the "removeEventListener" thing at the end of buttonClickResponse() doesn't work; I'm just using it as a placeholder.

JavaScript:
            } else {
                generatedQuiz[currentQuestion].answers.forEach(addButton)
                function addButton(thisAnswer) {
                    let answerButton = document.createElement("button")
                    answerButton.textContent = thisAnswer.text
                    answerButton.style.fontFamily = "Corbel"
                    answerButton.style.padding = "1em 2em 1em 2em"
                    answerButton.className = "option"
                    answerContainer.appendChild(answerButton)
                    answerButton.addEventListener("click", buttonClickResponse)
                    function buttonClickResponse() {
                        if (thisAnswer.isCorrect) {
                            console.log("I guess you're right")
                            clearInterval(questionCountdown)
                            correctAnswerCount++
                            nextButton.addEventListener("click", showNextQuestion)
                        } else {
                            console.log("u wrong")
                            clearInterval(questionCountdown)
                            nextButton.addEventListener("click", showNextQuestion)
                        }
                        answerButton.removeEventListener("click", buttonClickResponse)
                    }
                }
            }
As is often the case in vanilla JS, it's possible but convoluted. See the "Using an abort signal" section near the bottom of this page. Check out this browser compatibility table if you're worried about older browsers.
 
Okay i'm almost finished this but I'm stuck on one thing.

Is there a way in vanilla Javascript to remove multiple event listeners in this case? I used a forEach method to append a button along with an eventListener to each item in an array, and want to make it so that if you click one button, eventListeners for all buttons immediately go away (i.e. all buttons are disabled).

I tried making buttonClickResponse() disable itself by either setting it equal to "null" or redefining it as an empty func, but I got nothing. Also checked multiple stackOverflows and got nothing as well. And of course, the "removeEventListener" thing at the end of buttonClickResponse() doesn't work; I'm just using it as a placeholder.

JavaScript:
            } else {
                generatedQuiz[currentQuestion].answers.forEach(addButton)
                function addButton(thisAnswer) {
                    let answerButton = document.createElement("button")
                    answerButton.textContent = thisAnswer.text
                    answerButton.style.fontFamily = "Corbel"
                    answerButton.style.padding = "1em 2em 1em 2em"
                    answerButton.className = "option"
                    answerContainer.appendChild(answerButton)
                    answerButton.addEventListener("click", buttonClickResponse)
                    function buttonClickResponse() {
                        if (thisAnswer.isCorrect) {
                            console.log("I guess you're right")
                            clearInterval(questionCountdown)
                            correctAnswerCount++
                            nextButton.addEventListener("click", showNextQuestion)
                        } else {
                            console.log("u wrong")
                            clearInterval(questionCountdown)
                            nextButton.addEventListener("click", showNextQuestion)
                        }
                        answerButton.removeEventListener("click", buttonClickResponse)
                    }
                }
            }
It's been a while since I did JavaScript, but isn't this something you could elegantly handle by just having some extra state kicking around? E.g. a boolean questionAnswered that you set equal to true when a button has been clicked, and then you just update buttonClickResponse() to test whether questionAnswered is false before you do anything else in that function? I guess I don't see the point of dynamically destroying callbacks when you can just render them inert instead.
 
  • Like
Reactions: Marvin
Does anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.
 
Does anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.

Ditto for basically any language.
 
Okay i'm almost finished this but I'm stuck on one thing.

Solved it. Made "buttonClickResponse" go through the array again in order to replace all existing answer buttons with empty buttons that have no eventListeners attached.

JavaScript:
                generatedQuiz[currentQuestion].answers.forEach(addButton)
                function addButton(thisAnswer) {
                    let answerButton = document.createElement("button")
                    answerButton.textContent = thisAnswer.text
                    answerButton.style.fontFamily = "Corbel"
                    answerButton.style.padding = "1em 2em 1em 2em"
                    answerButton.id = "option"
                    answerContainer.appendChild(answerButton)
                    answerButton.addEventListener("click", buttonClickResponse)
                    function buttonClickResponse() {
                        if (thisAnswer.isCorrect) {
                            console.log("I guess you're right")
                            clearInterval(questionCountdown)
                            correctAnswerCount++
                            nextButton.addEventListener("click", showNextQuestion)
                        } else {
                            console.log("u wrong")
                            clearInterval(questionCountdown)
                            nextButton.addEventListener("click", showNextQuestion)
                        }
                        generatedQuiz[currentQuestion].answers.forEach(replaceButton)
                        function replaceButton(thisAnswer) {
                            document.getElementById("option").remove()
                            let emptyButton = document.createElement("button")
                            emptyButton.textContent = thisAnswer.text
                            emptyButton.style.fontFamily = "Corbel"
                            emptyButton.style.padding = "1em 2em 1em 2em"
                            emptyButton.className = "option"
                            answerContainer.appendChild(emptyButton)
                        }
                    }
                }

Does anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.

There's a guy on YouTube named "Alex Lee" who's basically a programmer who specifically covers Java stuff in quick and simple ways. I wanna learn more about it in the future after I learn more JavaScript since I've played around with it in the past. On top of that, it does hold a special place in my heart (no, not because of Minecraft, but because of some niche car game called "Need for Madness")

You can check out all his Java playlists here.

Although I'm not that interested in Python, I do know a guy who seems to cover it a lot, named "Tech with Tim". Here's a nice video that summarizes everything you need to know to master the language
 
Does anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.
Learn X in Y is a pretty good resource for starting to learn most programming languages, algorithms, data structures, and tools like Git in multiple spoken languages. Which is a fun way to teach yourself a foreign language imho. Each article on a langue will link you to the language resource code, give you an inline and commented example like cheat sheet, and links to additional resources like books, websites, sometimes tools at the end. Check out their Python, Java, or even the MATLAB(as a judgement gauge) articles to see if it appeals to your needs.
 
Solved it. Made "buttonClickResponse" go through the array again in order to replace all existing answer buttons with empty buttons that have no eventListeners attached.

JavaScript:
                generatedQuiz[currentQuestion].answers.forEach(addButton)
                function addButton(thisAnswer) {
                    let answerButton = document.createElement("button")
                    answerButton.textContent = thisAnswer.text
                    answerButton.style.fontFamily = "Corbel"
                    answerButton.style.padding = "1em 2em 1em 2em"
                    answerButton.id = "option"
                    answerContainer.appendChild(answerButton)
                    answerButton.addEventListener("click", buttonClickResponse)
                    function buttonClickResponse() {
                        if (thisAnswer.isCorrect) {
                            console.log("I guess you're right")
                            clearInterval(questionCountdown)
                            correctAnswerCount++
                            nextButton.addEventListener("click", showNextQuestion)
                        } else {
                            console.log("u wrong")
                            clearInterval(questionCountdown)
                            nextButton.addEventListener("click", showNextQuestion)
                        }
                        generatedQuiz[currentQuestion].answers.forEach(replaceButton)
                        function replaceButton(thisAnswer) {
                            document.getElementById("option").remove()
                            let emptyButton = document.createElement("button")
                            emptyButton.textContent = thisAnswer.text
                            emptyButton.style.fontFamily = "Corbel"
                            emptyButton.style.padding = "1em 2em 1em 2em"
                            emptyButton.className = "option"
                            answerContainer.appendChild(emptyButton)
                        }
                    }
                }
Is your goal to physically disable the buttons? or prevent them from doing anything?
 
Is your goal to physically disable the buttons? or prevent them from doing anything?

I am currently making a quiz application which includes multiple choice questions.

For the multiple choice questions, I used a forEach loop to add a button and an eventListener to each answer, which is contained in an array. Basically, what I wanted to do was make the click of one button disable all buttons' functionalities, including the one just clicked.

My goal was to remove all eventListeners upon one click on any of the buttons. So I basically accessed the existing buttons by calling its HTML id (i.e. "option") and removed them, then replaced those buttons with empty versions of those buttons that have no eventListeners, which are basically there to retain the appearance of question even though the eventListeners have been taken out
 
I am currently making a quiz application which includes multiple choice questions.

For the multiple choice questions, I used a forEach loop to add a button and an eventListener to each answer, which is contained in an array. Basically, what I wanted to do was make the click of one button disable all buttons' functionalities, including the one just clicked.

My goal was to remove all eventListeners upon one click on any of the buttons. So I basically accessed the existing buttons by calling its HTML id (i.e. "option") and removed them, then replaced those buttons with empty versions of those buttons that have no eventListeners, which are basically there to retain the appearance of question even though the eventListeners have been taken out
instead of removing and replacing the buttons, you should be able to do this:
JavaScript:
document.getElementById("option").onclick = function(event) { event.preventDefault(); }
 
Okay i'm almost finished this but I'm stuck on one thing.

Is there a way in vanilla Javascript to remove multiple event listeners in this case? I used a forEach method to append a button along with an eventListener to each item in an array, and want to make it so that if you click one button, eventListeners for all buttons immediately go away (i.e. all buttons are disabled).

I tried making buttonClickResponse() disable itself by either setting it equal to "null" or redefining it as an empty func, but I got nothing. Also checked multiple stackOverflows and got nothing as well. And of course, the "removeEventListener" thing at the end of buttonClickResponse() doesn't work; I'm just using it as a placeholder.

JavaScript:
            } else {
                generatedQuiz[currentQuestion].answers.forEach(addButton)
                function addButton(thisAnswer) {
                    let answerButton = document.createElement("button")
                    answerButton.textContent = thisAnswer.text
                    answerButton.style.fontFamily = "Corbel"
                    answerButton.style.padding = "1em 2em 1em 2em"
                    answerButton.className = "option"
                    answerContainer.appendChild(answerButton)
                    answerButton.addEventListener("click", buttonClickResponse)
                    function buttonClickResponse() {
                        if (thisAnswer.isCorrect) {
                            console.log("I guess you're right")
                            clearInterval(questionCountdown)
                            correctAnswerCount++
                            nextButton.addEventListener("click", showNextQuestion)
                        } else {
                            console.log("u wrong")
                            clearInterval(questionCountdown)
                            nextButton.addEventListener("click", showNextQuestion)
                        }
                        answerButton.removeEventListener("click", buttonClickResponse)
                    }
                }
            }
It looks like you've already solved this, but: yes, each event listener that you add can be removed, if you remove exactly the same function object that you added.

Since you define function buttonClickResponse() inside of the forEach (which is a new scope), it's being defined each time the forEach executes. And that could make it hard to track -- unless you're pushing them into an array that's outside that scope, or something, so that you can remove all of them.

Literally just moving the function up, out of the forEach and into a broader scope, so it's only defined once, could probably help in situations like this.
 
Does anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.
Python Crash Course is a good start for Python and free. It's worth a buy if it works for you,
Python Crash Course: A Hands-On, Project-Based Introduction to Programming, 2nd Edition

If you're a numbers nerd then data science with python may be interesting. Also free.
Python for Data Analysis. Data Wrangling with Pandas, NumPy, and IPython

Both Python and Java are so well documented and popular that you can look up any project you can think of and probably find material on it.
 
Has anyone read any of Zed Shaw's books like Learn Python The Hard Way? Are they worth it? I know he has an internet tough guy persona that's a little cringe there are a lot of people recommending his books.
 
Does anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.
Modern day SICP appears to be taught with Python nowadays, as opposed to MIT Scheme.
 
Has anyone read any of Zed Shaw's books like Learn Python The Hard Way? Are they worth it? I know he has an internet tough guy persona that's a little cringe there are a lot of people recommending his books.
Zed Shaw's books are good if you're willing to spend less time reading and more time solving problems. His teaching style is, "here's the barebones of what you need, now you figure out how to solve these problems (which will also give you the right hints to infer the most critical knowledge)"

He works because he filters out people who are incapable of problem-solving early in his curriculum unlike all the other books that you can 'learn' from but don't actually come out of knowing how to write software.

If he works for you, he works for you. Personally, I'd just watch an undergrad CS lecture series and do the problems. But that's not for everyone. You could certainly do worse than Zed "I named my HTTP server after my wife's son" Shaw.
 
  • Agree
  • Like
Reactions: naffatune and ditto
Does anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.
The Python tutorial on Python.org gives a good overview of the language. It might not be the best resource for learning to code but I'd specifically highly recommend reading it's chapter on classes and namespaces. Namespaces are something that always trip up new programmers and this article gives a really thorough explanation of how they work in Python


Also whenever I learn a new language I solve the ninety-nine prolog problems in that language.

 
  • Like
Reactions: Fcret
I am currently making a quiz application which includes multiple choice questions.

For the multiple choice questions, I used a forEach loop to add a button and an eventListener to each answer, which is contained in an array. Basically, what I wanted to do was make the click of one button disable all buttons' functionalities, including the one just clicked.

My goal was to remove all eventListeners upon one click on any of the buttons. So I basically accessed the existing buttons by calling its HTML id (i.e. "option") and removed them, then replaced those buttons with empty versions of those buttons that have no eventListeners, which are basically there to retain the appearance of question even though the eventListeners have been taken out
In JS you should be able to use removeEventListener method of each DOM element, but you need to have the function you are removing.
 
  • Informative
Reactions: Marvin
Has anyone read any of Zed Shaw's books like Learn Python The Hard Way? Are they worth it? I know he has an internet tough guy persona that's a little cringe there are a lot of people recommending his books.
no it sucks. has bad pedagogy and makes asinine statements.

your instinct about "internet tough guy" is right, he's just a blowhard.

just about the only good thing it teaches is the importance of not copy-pasting shit. you need to get into the habit of noticing all the little details your eyes would otherwise skim over, like the exact punctuation, exact whitespace and indentation used, capitalization vs lowercase, etc. all that stuff is there for a reason and the program will mean something else (or won't run at all) if you change it. if you copypaste from a tutorial you won't exercise that skill. when you're a beginner you should type everything out yourself, so you get used to always putting a colon after an if statement and always indenting for a new block (if your IDE doesn't do that for you).
 
Last edited:
Does anyone know how to pull git submodules in a tarball without a .git folder? The problem: I have download a tar.gz file with no .git folder, but it does have a .gitmodules file. The documentations says to run
Code:
git init && git submodule --init --recursive
but when I do this nothing happens. Does anyone know if I can add the submodules to the project using the supplied .gitmodules file? It looks like this:
Code:
[submodule "win32/libpng/src"]
    path = win32/libpng/src
    url = https://git.code.sf.net/p/libpng/code
[submodule "win32/zlib/src"]
    path = win32/zlib/src
    url = https://github.com/madler/zlib.git
[submodule "win32/DirectXMath"]
    path = win32/DirectXMath
    url = https://github.com/Microsoft/DirectXMath
[submodule "shaders/SPIRV-Cross"]
    path = shaders/SPIRV-Cross
    url = https://github.com/KhronosGroup/SPIRV-Cross.git
[submodule "shaders/glslang"]
    path = shaders/glslang
    url = https://github.com/KhronosGroup/glslang.git
[submodule "vulkan/vulkan"]
    path = vulkan/vulkan
    url = https://github.com/KhronosGroup/Vulkan-Headers.git
I really just need the glslang and spirv-cross ones.
 
Back