Posts by Luiz Felipe • 32,886 points
746 posts
-
13
votes2
answers469
viewsA: What are the logical assignment operators ||=, &&= and ??= in Javascript?
Are the logical allocation operators. In Javascript, they were standardized from Ecmascript 2021 based on the call Logical assignments Proposal. These are the three new logical allocation operators:…
-
2
votes2
answers131
viewsA: Inserting objects into an array through a loop
I think you need to understand better what a function javascript. Let’s check this part of the code: aniversario.push(i) = { nome: 'Thiago', idade: 20 diaDoAniversario: '7/12/2020' } Note that you…
-
2
votes1
answer39
viewsA: Doubt about waiting for a Javascript function to return
What happens is that its function gServidor is not returning anything. Therefore, if you call, nothing will be returned, although the code is actually running within hers. You need to use the…
-
8
votes1
answer141
viewsQ: What are the differences between "String" and "str" in Rust?
I know there are (apparently) two main types of string in Rust: str, which, as far as I can tell, is a primitive type; String, which, as far as I know, is part of the standard library std of…
-
2
votes1
answer81
viewsA: How to make several elements participate in Event Listener?
This happens because, although each button has a attribute Event Listener, they all perform the same function (in this case, the function clique). However, note that the function clique operates…
-
2
votes1
answer114
viewsA: What does arroba (@) mean in the name of a dependency on the package.json file?
It means the package is inside a organization of npm. Most often the organization is public (as well as the respective packages). Take, for example, the package @fullcalendar/interaction. Actually,…
-
10
votes2
answers304
viewsA: What are the differences between match and switch in PHP8?
The statement switch Basically, the switch is a statement (in English statement) which allows the programmer to control the code flow. The switch decides which "arms" will be executed based on a…
-
2
votes2
answers46
viewsA: How to obtain a single object from the comparison of the smallest of the values between an array of objects?
The problem is, when applying Math.min to the result of mapping the original array to the prices and delivery times, you lose the reference to the desired object. In this situation, although you…
-
7
votes1
answer150
viewsA: How to remove a file from Git, but keep it locally?
You don’t even have to go through the Github interface. You can do everything locally. Basically, you use the command git rm: The problem is that by using the git rm "alone, "the file will be…
-
3
votes1
answer55
viewsA: How to avoid duplicate Javascript addeventlistener calls?
If you add event listeners in Javascript, you add one at a time. Because there is no addEventListeners, only addEventListener (in the singular). This duplication is rarely problematic, but if you…
-
5
votes1
answer108
viewsA: Breaking text with regular expression in Javascript
Depending on the complexity of the HTML tags that are contained in your string, do this with regex can no longer be trivial. It is almost consensus, too, that regular expressions should not be used…
-
10
votes3
answers194
viewsA: Why does isNaN(null) return "false"?
Why isNaN(null) returns false? Because null can be converted to a numerical representation in Javascript. In this case, null is "equivalent" to the 0 after coercion. The difference can be noticed…
-
4
votes1
answer2509
viewsA: Problem counting how many times a character repeats in a given string
The first problem is that you are not returning anything from the function vezesLetraAparece. Note that there is no statement return in the body of the function and, because of this, it at all times…
javascriptanswered Luiz Felipe 32,886 -
5
votes0
answers90
viewsQ: What is the Utility-first standard in CSS?
In CSS, there are patterns like WELL for the organisation of the nomenclature of the HSC classes. Recently, with the rise of libraries like the Tailwind CSS, I realized the existence of a new* CSS…
-
4
votes4
answers2518
viewsA: How to count occurrences of a value within an array?
The other three answers count how many times one string repeats within an array, but in the possibility of several strings repeat? In this case you could "adapt" the code of the other answers and…
-
2
votes1
answer20
viewsA: Function error not set when working with Ecmascript modules in the browser
When you add the attribute type="module" in a script, it is no longer treated in the conventional way. Therefore, the functions (globally) stated in it are not accessible to HTML. That’s why you…
-
2
votes1
answer60
viewsA: How do I make the program identify if the split denominator is zero using switch-case and display an invalidity message in the operation?
I can use if inside switch-case? Can. The case of a switch statement in C is not unique to expressions. You can put statements (statements) within them as well. This includes, for example, a if. The…
canswered Luiz Felipe 32,886 -
3
votes1
answer194
viewsA: How is the scope of an Event Listener defined in any attribute in HTML created?
TL;DR The element containing the attribute Listener is used to derive the scope of the function that will be executed. That’s why the element properties (such as textContent, value etc) can be…
-
5
votes1
answer194
viewsQ: How is the scope of an Event Listener defined in any attribute in HTML created?
According to the documentation, when the Listener event is assigned as an HTML attribute, the specified code (in the attribute value) is wrapped in a function, with the following parameters: event -…
-
2
votes1
answer71
viewsA: What is and what is the "Proxy" Javascript constructor for?
The object Proxy is a means to achieve a certain type of metaprogramming in Javascript. In the field of metaprogramming, there are three major categories: Introspection; Own modification;…
-
2
votes2
answers73
viewsA: How to declare that an object in a Javascript array is empty
Bearing in mind that empty strings ("") in Javascript are values falsy, you can use vagas[lugar] directly on if without the need for any kind of comparison. This happens because the if, when testing…
-
2
votes1
answer77
viewsA: Can I use Object.assign to assign properties to this in Javascript?
Yes. As I explained in this answer, the Object.assign uses internal operation [[Set]] to set the properties of objects passed from the second argument onwards to the object passed as the first…
-
5
votes1
answer49
viewsA: How to expose a set of functions that is within an object in the global scope?
I really wouldn’t advise it. First because you can pollute the global scope or even overwrite an original property in case of name conflict. However, if you really have a good reason (which I find…
-
1
votes1
answer37
viewsQ: What is and how does the "Atomics" object work in Javascript?
According to the documentation: The object Atomics provides atomic operations as static methods. They are used with objects SharedArrayBuffer. But it doesn’t seem clear to me. I would like to know…
javascriptasked Luiz Felipe 32,886 -
5
votes3
answers136
viewsA: How to count from "1" instead of "0" in a Javascript array
Do not use the for..in, which should be used to iterate on the keys enumerable of an object. Use a for normal and, at the time of printing, add 1 to the counter (i). Something like that: let vagas =…
-
2
votes1
answer114
viewsA: How to format an array for JSON output?
It should be mentioned first of all that there is no need to use JSON.parse there (assembling the JSON string in a literal way), since this is a less performatic medium and more susceptible to…
-
2
votes1
answer25
viewsA: Objects with equal name fields using scatter notation (spread Operator)
It is not that you cannot use scattering notation on objects that contain names of equal properties. It can and is, in fact, being done. The problem is that objects cannot have two properties with…
-
1
votes3
answers520
viewsA: Why do line breaks between elements cause space between them?
Why does this happen? This difference happens because, when displaying HTML, the browsers condense a sequence of blank characters (i.e. spaces, line breaks, etc) into a single space. This behavior…
-
1
votes2
answers128
viewsA: Put a variable inside a spin?
First, we must create the stirng with the pattern to be used dynamically. There are several ways to do this: Use concatenation You can concatenate strings in Python using the operator +. Thus: nome1…
-
5
votes1
answer38
viewsA: How to return only the last commit date?
You can use the option --format, using the placeholder %cd, thus: git log --format=%cd That returns only the dates for the latter commits. To return the date of the last commit, just use the range…
-
2
votes2
answers53
viewsA: How can I exchange elements of a string using the values of an object as argument?
Of course, you can use regular expression, as suggested by another answer, but you can also divide the phrase by spaces and exchange the "operator-words" for the respective symbols. Behold: const…
-
2
votes1
answer51
viewsA: Search a String in an Input Forms
When you use methods like document.getElementById, you will receive some DOM element. In case you select a <input />, the returned element will implement the interface HTMLInputElement. Note…
-
7
votes1
answer147
viewsA: What is the equivalent of Python’s dir function in Javascript?
This is because, in fact, methods such as sort do not belong to the builder Array, but yes to the Array.prototype. Note, in the exit code you ran, that the Array has a property called prototype.…
-
9
votes2
answers641
viewsA: When "Return" is different from "Return await" in an asynchronous function in Javascript?
Briefly, the return is different from return await when you’re inside a block try. In the specific case of the question, there is no difference. There, return is the same as return await because…
-
4
votes1
answer37
viewsA: Why is Math type Object in Javascript?
According to the specification, a value of the type "object" is (in free translation): An object is a collection of properties. [...] Therefore, Math is an object since it is, in fact, a collection…
-
4
votes1
answer139
viewsA: I cannot invoke "innerHTML" to change the HTML of an element. Why?
See in your code: res.innerHTML(`A média do aluno é: ${m}`); innerHTML is not a function to be called (note the parentheses, which denote the function application syntax in Javascript). Note that a…
-
7
votes2
answers191
viewsQ: What are the main differences between prototype-oriented programming and class-oriented programming?
After learning a little more about Javascript, I realized that even though I have a building class, classes (in fact, as in C# or Java) do not exist in Javascript. This is nothing more than…
-
6
votes1
answer459
viewsA: How to put objects in empty array with React Hooks
The problem is that you boot data as a array which already has the first element filled in. In this case, by a literal object "empty": const [data, setData] = useState([{}]); // ↑↑ // Objeto "vazio"…
-
1
votes2
answers162
viewsA: When using the term "prototype" to refer to a Javascript method, as in "Array.prototype.foreach"?
In this answer, I will use the examples Array.prototype.forEach and Array.from (didn’t use Array.forEach because it does not exist). However, the same holds true for any other constructor function…
-
5
votes2
answers162
viewsQ: When using the term "prototype" to refer to a Javascript method, as in "Array.prototype.foreach"?
Javascript has several objects with methods, as is the case of arrays which have, for example, the method forEach. But eventually the same name can be used by other objects, such as method forEach…
-
6
votes2
answers266
viewsA: Problem with reduce to calculate arithmetic mean of array
It is even possible to do all this using the reduce, but it is not ideal, since there is a need to create a code more complicated than necessary. To another answer explains why the original code…
-
1
votes1
answer160
viewsA: Type int variable output error in Dart code
According to the documentation, the method int.parse must receive a string in its first argument. However, you are not passing any value to the parse can work (which causes the error). To correct,…
dartanswered Luiz Felipe 32,886 -
2
votes2
answers43
viewsA: How can I turn a string of numbers into a Matrix?
With the str.split('\n'), you are splitting the original string a array with three other strings, separated by '\n'. However, for each of these three new strings, you must separate them again so as…
-
3
votes2
answers123
viewsA: Is it possible to delete the browser history?
It is not possible to do this (because, if possible, it would represent a kind of "security risk" for users). It is worth noting that the history.length computes the entire history of session user…
-
2
votes1
answer201
viewsA: How to resolve or reject a Promise (native) in Javascript outside of your scope?
You could do this kind of gambit: const controls = {}; const promise = new Promise((resolve, reject) => { controls.resolve = resolve; controls.reject = reject; }); promise.then((val) => {…
-
2
votes1
answer376
viewsA: I’m having an error Cannot read Property 'classList' of null at initialModal, but I don’t know how to resolve
Note that your script performs the function iniciaModal immediately (as soon as this part is evaluated). Note also that you entered the <script> in the <head>, which shows that the…
-
2
votes1
answer61
viewsA: Different ways to create an iterator for values
According to the specification of Array.prototype[@@iterator]: The initial value of the property @@iterator is the same function-object as the initial value of the property Array.prototype.values.…
-
3
votes2
answers60
viewsA: Express - Suggested experience, how to handle this type of code repetition
First, regarding the repetition of "validation", you can do something like suggested in the other answer: ['username', 'password', 'email', 'fullName', 'birthDate'].forEach((key) => { if…
-
4
votes1
answer667
viewsA: What does the "git branch -M" main command do?
According to the command documentation git branch, to flag -M assumes the role of --move together with --force. This means that when executing the command, the branch will be renamed to main, even…
-
5
votes1
answer144
viewsA: Why Arrays and Functions are Objects?
According to the language specification: An Object is logically a Collection of properties. Which, in free translation, means that an object is, from the logical point of view, a collection of…