While I was coding, I have noticed that I am repeating the same code twice in two callback functions: I have tried to write some algorithm which allows me to implement this goal, but still I am unable to reach that goal. Answer Introduce a new function and move the common logic to that function. Please have a look. I
Tag: javascript
How do I search through an array using a string, which is split into an array?
I currently have a half solution to this, but I was wondering if there is a better way to write this. I have the following array with equipment deliveries: Which I search through using an input: What I’m trying to achieve is to split the search word with spaces, and check if an index in the array contains all of
Service Workers – selectively choose resources
I have a simple Service Worker implementation like this: My goal is to cache all resources except the two files indicated in the urlsToCache variable, these two files should ALWAYS be requested from the network.. How can i achieve this? How can I then verify that it is actually so? Answer You can check the url of the request in
Count characters of dynamically created textareas with different id and name attr
I am having two textareas one under another Both textareas are created dynamically and they have different id and name attr. At some point, they might be 2, 3, 4… and so on. What I am trying to do is to create a char counter for each textarea that also applies dynamically. It would have had been easy if the
Save ID in variable and add style to it
I want to highlight randomly a word which is inside a <p> tag with the id=JShighlight. It should blink one time and then another randomly chosen word should blink.** I can’t add the css style needed to the ID. The words are nested in a span with an unique id like id=”one” oder id=”two”. Like: To chose a word randomly,
Why doesn’t my JS update my data-theme back?
I want to have a button inside my navbar (ul li) that can toggle between theme=light and theme=dark. However, only activating dark mode works. Code: I created a button <li><a href=”#” onclick=”darkMode()”><i class=”fas fa-moon”></i></a></li>, with a JS function and my css looks like this: Answer 1. You start the function by setting the isOn to false. so Boolean(isOn) === false
Can’t find variable: counter, in expo
Been using React for a bit and wanted to try out native using expo, when I click on a button to increment a counter, I get a ReferenceError on my phone saying “Can’t find variable: counter”, I don’t get any error on the expo gui or in VSCode, very confused. Answer Change your increment function to Make sure to define
Sortying array by two criteria
I have array like this: And I’m trying to sort this array so elements that are isImportant: true with the most recent date are displayed first and then elements that have isImportant:false with the most recent date Answer I hope this will resolve your issue
How to alter a API response with Javascript
I wrote this little app that takes in a set of starwars characters and returns images in an array. The result of the above code looks like this : But i would like a result where i can know which image belongs to which keyword. Maybe a format like this or something similar : I tried this : Results were
My css code in Word Press is working for every other page in the website when i only want it to work for 1 page
I have currently made a code in CSS, which I wanted for a particular page but unfortunately, it is working for every other page as well and it makes the other pages look quite broken since the formatting of them goes very odd. The solutions I have tried so far are: Using the unique code of the page and making