Posts by Sergio • 133,294 points
2,786 posts
-
1
votes1
answer46
viewsA: Object Assign cloning undefinied properties
The idea of using ...myArray in the Object.assign is good but as you noticed vis always overwrite old values with new ones, and end up overwriting the value you want for an empty string. You have to…
javascriptanswered Sergio 133,294 -
2
votes2
answers56
viewsA: Problem with toFixed() Javascript method
You can use the toFixed with decimals plus and then cut the last value... const toNotRoundedFixed = (nr, fixed) => nr.toFixed(fixed + 3).slice(0, -3) const num = 10.125…
javascriptanswered Sergio 133,294 -
1
votes1
answer42
viewsA: The setState is not working
The setState is asynchronous... when you invoke this.setState(... change is not immediate. This is the React paradigm, never assume it is immediate (asynchronous). Prepares the code for "empty"…
-
0
votes1
answer465
viewsA: How to redirect to another route and pass variables in the process? Node + Express
The .redirect is used to redirect the response, not the request (request). You have to do this by calling in internal functions. That is instead of the logic of redirecting you must, within that if…
-
2
votes1
answer347
viewsA: How to upload files with Multer to two different storages?
Instead of letting the woman run "alone" you can create your middleware and intercept the answers of the woman. I assume the problem is that the multer overwrites the req.files, I’ve never tested…
-
0
votes1
answer217
viewsA: Parsing error: Unexpected token React
Puts this.converter = this.converter.bind(this); within the contructor (after super. Or alternatively use like this, Arrow Function directly to maintain the this in the class instance: import React,…
-
4
votes2
answers847
viewsA: How many days a week do you have in a month?
I made a library to generate dates, number of week etc that I use in my projects, can be useful to you. In this case it is simple and you can do it in Javascript. Note that this script is…
-
0
votes1
answer54
viewsA: Access Array within Array
Think this outer array has A, B, C. To access these values would be respectively, numeros[0], numeros[1], numeros[2]. Continuing this reasoning, numeros[0][0] would be the 01 which is within the…
javascriptanswered Sergio 133,294 -
1
votes1
answer510
viewsA: Search using Javascript only in all table fields
You can do it like this: const input = document.getElementById('filtro-nome'); const trs = [...document.querySelectorAll('#lista tbody tr')]; input.addEventListener('input', () => { const search…
-
3
votes2
answers170
viewsA: I want to generate 4 random values from an array. How can I do it?
You can do it like this (notes in code comments): const getRandom = (arr, nr) => arr .slice() // criar uma cópia para não mudar a array inicial .sort(() => 0.5 - Math.random()) // misturar…
-
0
votes1
answer141
viewsA: How to insert a value into an input and dynamically change an element through it?
Here’s a suggestion with input type="number" and who uses the event input to call the function that applies the values. I refactored your code to make it more compact and easy to maintain. const…
-
1
votes2
answers153
viewsA: Display the result of a calculation on the screen
Some considerations: do not access DOM elements by ID this way, take a look here: /a/123106/129 instead of if/Else you can have an array of values and iterate that array. Actually in your if/Else…
-
1
votes1
answer46
viewsA: How to calculate the total in a filter in Vue js
Suffice it to add .reduce((acc, item) => (acc += parseFloat(item)), 0) .toFixed(2); at the end of that computed. You can adapt the method somaTotal to receive an array and do so: methods: {…
-
2
votes1
answer93
viewsA: How to force error in the return of a Promise inside another Promise
Two options: withdraw that catch return a new error within the catch. The async/await API is very practical but creates difficulties with error management. async function teste() { const res = await…
-
1
votes1
answer22
viewsA: Check if the value of an object of a certain class has been changed
You can use getters to do this. These values of this.valores can be a write protected (read-only) value pointer this.valor1 and this.valor2. You can do it like this: class minhaClasse {…
-
1
votes2
answers636
viewsA: Return of a Knex query using . map
Some things c consider: This method/function index does not seem to need feedback as you are sending the answer within it. In that case async is not necessary and you can treat everything "in the…
-
2
votes2
answers332
viewsA: How to change the color of only the clicked element?
You can do this with classes. Adding and removing with el.classList.add( and .remove(. But since you’re doing it directly in the element property what you’re missing is, within a loop, reset values…
javascriptanswered Sergio 133,294 -
1
votes1
answer1417
viewsA: How to create an object array with React useState
What you are looking for is an Array, not an Object. As you are doing you are creating a single object with 3 keys (idWebservice, nameWebservice, addressWebservice) that are always being rewritten.…
-
8
votes2
answers195
viewsA: What is the logic behind the switch case in Javascript?
It is a characteristic of the language each case may have break. This is how language was drawn. When a break the switch is interrupted. The case is a label, acts as a pointer to the statement where…
-
6
votes1
answer51
viewsA: Change the native console function.log
You can... but I advise against it. Other people using your code will have serious problems finding where that method was over-written. Having said that, you can for example add >>> to all…
-
2
votes1
answer72
viewsA: Javascript - . map()
If you just want to modify the Indice 2 just do so: nums[2] = nums * 2; So you change the internal value of the array without needing to reassign. If you want to use .map() which is the best…
javascriptanswered Sergio 133,294 -
0
votes1
answer35
viewsA: how to pass a javascript phrase to html
In Javascript variable names and functions occupy the same space, ie overlap. So you have to give a different name to the variable or function. Besides, to run a function you have to use () to the…
-
5
votes1
answer66
viewsA: MP3 does not work in Javasccript
Chrome has changed the behavior relative to video and audio to prevent the sound from starting automatically by opening a page. Both in audio and video it is now only possible to start "producing"…
javascriptanswered Sergio 133,294 -
0
votes1
answer35
viewsA: I made a calendar and the dates are wrong
The problem is here: function diasDoMes(mes, ano){ return new Date(ano, mes, 0).getDate(); } Instead of having (ano, mes, 0) you must have (ano, mes + 1, 0), the reason is that 0 in the position of…
-
1
votes1
answer1695
viewsA: How to add values of an object array[JS], with only functions and loop for?
"a program that calculates the sum of user income and expenses" - my interpretation is that the intended one is an array with the sum of each user. To be so needed a .map because you want an array…
-
2
votes1
answer21
viewsA: Why do some npm packages have a '@' precedent in the account name and others not in the package-json deps list?
This is an NPM Feature that allows namespaces in libraries. At work we have about 6 libraries of our own that we load in several projects. Besides being practical to see where it comes from it also…
-
2
votes1
answer140
viewsA: Intersection of 2 arrays with objects and arrays in object property
You can do it like this: checks whether all array entries have objects whose keys have the value included in the corresponding array in the compared object. const A = [{…
-
0
votes1
answer220
viewsA: How to save asynchronous function return solved
You can only use the await within a function async... if that code runs on the global scope (window, or the root of a module) then you have to wait/use the .then and only then will you have the…
-
2
votes2
answers63
viewsA: Toggle the text of an element with each click
It’s one thing to register an event headphone document.addEventListener; another thing is to activate it. To activate an event handset you need to click on the page (in this case the event click) or…
-
2
votes1
answer118
viewsA: Global window integrity in Javascript
Some ideas that are used to make the code more secure (the first refers to your example, and properties of window) : records the window properties as immutable At work we did so: const…
javascriptanswered Sergio 133,294 -
3
votes1
answer37
viewsA: Reduce to the Object?
reduce is not the proper method for this. It is not because it will only iterate the number of times the length of the array you are given. What you need is a recursive function, which calls itself…
-
1
votes1
answer96
viewsA: How to delete items from HTML using Javascript
Some problems in your code: uses buttons, is more semantic and beyond the event submit has to be stopped to avoid reloading the page const produto = document.querySelector(".produto"); loads 1…
-
0
votes1
answer82
viewsA: React :: exchange part of a string for tag input
You need to pass a JSX array. Use the .split to remove pieces you don’t want and then add to that array the elements you need (for example with a reduce. It would look like this (example): const App…
-
2
votes2
answers273
viewsA: Why does pointing to the same memory address change an object?
When you use the let (or var) you give a value to that variable. When you assign a new value to that variable it loses relation to its previous values. That is: let a = 2; a = 3; // dá 3 Similarly…
-
0
votes4
answers118
viewsA: How to reference a JS item from a DOM element
Yes, you can pass this as argument, or use this event and read the property target which will be the element of the DOM clicked. Would that be: const controller = { conclui(evento, button, algo) {…
javascriptanswered Sergio 133,294 -
0
votes1
answer58
viewsA: How to use javascript to perform math operation
Following your mathematical logic, you can use the Math.floor to obtain the rounded whole part below, and the Math.ceil to round up. Then use the % to extract the decimal part (with some extra code…
-
0
votes1
answer117
viewsA: How to get the next attribute of an object array?
You can use the .findIndex to search the array for the index of that name/display you have. Then add +1 and you have the next. Note that when you reach the end of this array (when there is no next…
-
4
votes1
answer176
viewsQ: How to use . reduce with async/await?
In synchronous code I can have a .reduce to make a sum calculation (as in this simple example of a shopping list): const tipos = ['leite', 'manteiga', 'pão']; const precos = [23, 21, 32]; const…
-
4
votes1
answer176
viewsA: How to use . reduce with async/await?
The problem (after half an hour) is that the callback of the .reduce, being asynchronous, returns a Promise... i.e., the first callback argument (after the first iteration) is a Promise. In order to…
-
0
votes1
answer43
viewsA: Return the value of an async function
You can only use await within asynchronous functions. To use otherwise you have to use the Promise API. I mean, when you use it like that, it’s wrong: async function foo() { return 'bar'; } const…
-
1
votes1
answer149
viewsA: Recursive function JS
You have to give return in that epar(x-2), otherwise the function returns nothing and this recursive cycle does not pass the result out. function epar(x) { if (x == 0) { return true } else if (x ==…
-
0
votes1
answer36
viewsA: Use includes() in a filter()
The method .filter returns a new array, filtered. For this to work you have to return a Boolean value (true or false) inside the callback of this filter to indicate to .filter whether or not to…
-
1
votes2
answers55
viewsA: How to use Reactdom to create Buttons through conditions
You can treat these buttons as an array and use if to add buttons to that array. Then to insert these buttons into the component just {botões}. Note that your array as it is "hardcoded", always has…
-
0
votes1
answer144
viewsA: Run a python script in javascript
You can use to run Nodes. I’m not sure what the logic of your application is and what asynchronous threads you need to have but an example would be: const { spawn } = require('child_process'); const…
-
2
votes2
answers115
viewsA: Word most often in a string from a size
This takes several steps... I leave a suggestion. Basically: separates the numerical part of the rest interprets the value of the number creates a regex to catch the expected length creates an…
javascriptanswered Sergio 133,294 -
3
votes1
answer50
viewsA: What is the behavior of two anonymous functions in an object?
I highly recommend writing code that way. Because the comma operator generates hard to read code, and because passing prototypes as arguments of functions will generate complexity and consequently…
-
1
votes1
answer44
viewsA: How to look for a value within an arrangement
A loop for will always be stopped when you call the return. When you have an input and you want to turn it into something else, you’re mapping it. That’s what you’re looking for here, mapping names…
-
6
votes3
answers195
viewsA: Why do I need to spend two then on AJAX requests made with the fetch API?
The return response of the fetch is a stream, and to extract its contents (Body.json() in API) is used response.json() which is asynchronous and returns a Promise itself. So or if it uses…
-
1
votes1
answer79
viewsA: How to get the "end" event from http with fetch API
The fetch and the request has different Apis. fetch returns a file and state control is done by API of the Promise with the .then, .catch and .finally; while the request works with events. So you…
javascriptanswered Sergio 133,294 -
0
votes1
answer66
viewsA: I’m not talking about displaying Github’s content on screen, so according to the exercise, someone will help me?
Your problem is that the tag <script> is not closed. You have to have <script></script>. Plus you can simplify the code and do something like this: var userInput =…