Posts by Sergio • 133,294 points
2,786 posts
-
1
votes1
answer132
viewsA: Data synchronization, mysql and Promise
This is a typical case of a loop forEach asynchronous code running. I talked about this here, here and here. I recommend giving a reading to better understand the problem. In your case you can use…
-
1
votes1
answer138
viewsA: Node.js First steps, don’t know how to start
This onrequest function will be called each time there is a request to the server. You have two parameters in that function, the first is the object of the request (request) and in it you have a…
-
2
votes1
answer41
viewsA: addeventlistener being called irregularly
You’re running the function when you have it retornaPai() as an argument from addEventListener... you have to take the () to pass a reference to the function and not call the function even before…
-
1
votes1
answer34
viewsA: Changing the box color with Javascript
You have to generate a new color every click, that is inside the onclick or of onmouseover (depending on the effect you seek)... Mute var newColor = getRandomColor(); thus: function getRandomColor()…
javascriptanswered Sergio 133,294 -
1
votes1
answer308
viewsA: How to add a LI to a UL via Javascript?
Javascript does not have a method to insert "after", but has "before". So the code that would do what you need is elementoAnterior.parentNode.insertBefore(novoElemento,…
-
1
votes1
answer36
viewsA: Function looking for a node that satisfies a callback
That first if, like you said, it’s a backup to only run the rest of the code if callback is a function. Use Object.prototype.toString.call(algo) it’s common to find out the kind of thing we went…
javascriptanswered Sergio 133,294 -
2
votes1
answer257
viewsA: How to navigate a dynamic javascript tree by setting the value of its properties?
The simplest way is to convert this object into String and change the text with a Regexp. Have the added advantage of not changing the initial object. const obj = [{ "roleName": "CCA", "children":…
-
4
votes2
answers199
viewsA: Javascript does not work in IE 11
IE11 does not support Arrow functions, classList (some versions) or -forEach in collections, only in Arrays. You have to use older Javascript. You can change the code to something like this: ()…
-
1
votes1
answer85
viewsA: How to merge properties of Javascript objects
You can use string arrays or concatenation. An example with array would be: for each partial: [1, 3].join('x') that gives 1x3 to join the partials: [[1, 3].join('x'), [2, 3].join('x')].join('|')…
-
1
votes1
answer596
viewsA: How to display style and run javascript along with html on Node Js?
The server serves one file per request. It can serve more than one at a time, but it will be with parallel requests where only one file is sent per request. In practice this means that you should…
-
3
votes1
answer417
viewsA: Async/Await does not work with callback
There are different ways to chain asynchronous functions. In this case toDataUrl() is an asynchronous function that uses callbacks. This cannot be combined with async/await that way. That is, or use…
-
2
votes2
answers218
viewsA: how to reuse HTML structure and change content (avoid Ctrl c Ctrl v)
Sort this content in an object array. Something like this: [{ titulo: 'Jogadores', descricao: 'Os melhores jogadores do mundo', items: ['Ronaldo', 'Messi'] }, { titulo: 'Cores', descricao: 'Cores…
-
2
votes1
answer162
viewsA: Use of "Return" in module.Exports
Your job query is not asynchronous in a modern way, but rather via callback. So you have to encapsulate the function in a Promise and call the resolve of Promise when callbacks are called. You can…
-
4
votes2
answers205
viewsA: List Attributes of an HTML Element with JS
You can use the Element.attributes that gives you an eternal object with nodes where you can extract the key and value of each property. An example would be like this: const input =…
-
3
votes1
answer47
viewsA: Doubt with scope js
The this is not a variable, is the value corresponding to the execution context of a function. It is always present and cannot be declared. In your case as the function does not belong to any object…
javascriptanswered Sergio 133,294 -
4
votes1
answer84
viewsA: Is there a problem using objects like this in Node.js?
Declaring constants before or having the require directly inside the object is irrelevant in terms of performance. It is good practice to have the requires declared at the beginning of the file, so…
-
1
votes2
answers255
viewsA: How to make Javascript return a given result using the for.. of loop and the Join method?
Note: Here at Sopt are not welcome questions of the style "do this homework for me", but questions of who wants to learn are very welcome. Schematically what you need to solve this problem is, by…
javascriptanswered Sergio 133,294 -
7
votes7
answers4295
viewsA: What does it mean Does the solution make comparisons against strings in the following code? JS
What you did is right. I see no need to compare otherwise in an exercise than somewhere having a string. What you can do is declare the right answer also in a variable and then compare those…
-
2
votes1
answer16
viewsA: jQuery - Effect of transforming text into tag only works on the first row of the table
In HTML the attribute id must be unique. You can only have 1 per page. You are creating duplicate ids for each row in the table... Change these id to classes for example and jQuery changes the logic…
-
3
votes3
answers4717
viewsA: I need to write a suit functionDeruco, that given a suit, returns a list of strings, one for each card of this suit following the cards of the truco
The most logical way, since the type of cards is pre-played is to have an array "prepared" and return only with suit indication... ie: function naipeDeTruco(naipe) { return ["1", "2", "3", "4", "5",…
-
9
votes5
answers3418
viewsA: Check the amount of negative elements in a JS array
What you need is: function quantidadeDeMesesComPerda(umPeriodo) { return umPeriodo.filter(nr => nr < 0).length; } const test = quantidadeDeMesesComPerda([10, -10, 2, 100]); console.log(test);…
-
2
votes2
answers69
viewsA: Summing Table Line Values
Don’t need and jQuery for that. Basically: listen to the event input in each tr extracts the elements takes account of the fact that , in Javascript should be . formats the total with the locale…
-
1
votes1
answer63
viewsA: don’t know where to start someone to help me?
I read that question like this: Create two arrays, with integer numbers. Then generates a new matrix where each position contains the sum of the respective numbers in the other matrices. Then…
javascriptanswered Sergio 133,294 -
2
votes2
answers145
viewsA: Return certain array values
You can use the .slice(0, 5) which gives you exactly the first 5. In other words, create a new array with only a part of the initial array. The API of .slice is .slice(<indice onde começar>,…
javascriptanswered Sergio 133,294 -
4
votes1
answer68
viewsA: Create a <div> inside a . catch in Aces
The correct way is not to put a div inside the catch, but to change the state for this div to be shown... So no catch usa: componentDidMount() { axios.get(API_ListaEmpresa) .then(response => {…
-
1
votes1
answer30
viewsA: I want to make a button that changes the background of the page. I wrote in js but I don’t know why it doesn’t work
3 things wrong with your code: In HTML you cannot comment attributes, remove this /* */ The estate background-color in Javascript can not have -, must be written in camelCase, that is to say:…
javascriptanswered Sergio 133,294 -
4
votes1
answer57
viewsA: How can I refactor this vuex Mutation?
You can do it like this: const changeQuestionState = (state, type, {id}, typing) => { return state[type].map(_question => { return { ..._question, typing: _question.id == id ? Boolean(typing):…
-
0
votes1
answer137
viewsA: Views not loading express/Node.js
You are exchanging tools that Express.js offers you. The app.use([path,] callback [, callback...]) wait as first parameter o url or a Regexp to handle urls. Then to display the tab you use the…
-
3
votes1
answer41
viewsA: Lexically, what happens when we return a variable within a method that is declared in the function of that method?
Each function creates its scope. The var when declared/used within a function declares that variable and blocks access to variables with the same name outside that scope. However, in the lines…
-
0
votes1
answer52
viewsA: how do I handle DOM with Foreach using HTML Collection iteration correctly?
Your code is correct but it is running too early. You have to put the code at the end of the HTML so when it runs the HTML it has already been read. Another alternative is a function similar to…
-
4
votes2
answers175
viewsA: I wonder why this Javascript code is not working (simple program)
Some mistakes: the getElementById wants only the ID, as you have input#verificador is a full CSS selector and can work for example with the `querySelector`` uses Document only and…
-
6
votes3
answers1319
viewsA: How to create a list from a JSON with Javascript?
You can use the reduce to do that: const animais = dados.animais.reduce((obj, animal) => { const tipo = animal.tipo + 's'; const {nome, id, tags} = animal; obj[tipo] = (obj[tipo] ||…
javascriptanswered Sergio 133,294 -
2
votes1
answer37
viewsA: Class method returns null
Class methods shall be declared with : () => { or abbreviating directly to (){... or as we would do in an object. in addition, the syntax of that data that you pass to class when instances are…
-
3
votes2
answers310
viewsA: Is it possible to check if an element does not have the style attribute with jquery?
In jQuery it is possible to use var visivel = $(elemento-ou-seletor).is(":visible"); var naoVisivel = $(elemento-ou-seletor).is(":hidden"); This checks if the element has display: none; or not.…
-
0
votes1
answer42
viewsA: Creating a list of elements traversing an associative array in Ecmascript
Aluns errors of logic: you have to use the createElement once for each `li`` the createElement is a method of document to add content you can use the textContent uses .forEach the .map cannot insert…
-
3
votes1
answer68
viewsA: Change CSS automatically when typing
This type of functionality is complex. Mainly because of the position of the marker/cursor... If you are writing in the middle of a sentence and change the content (by changing the {{AMIGO}} it is…
-
2
votes3
answers168
viewsA: Add and remove class when using pure Javascript getElementById
You cannot have more than one element with the same ID on a page. You have to use classes and you can do it like this: function activeClass(clicado) { const clicaveis =…
javascriptanswered Sergio 133,294 -
2
votes1
answer882
viewsA: What does this reticence mean in the array?
That is spread syntax and is a simple way to convert something eternal into an array. When using querySelectorAll for example, returns something similar to an array but which is not a truth array.…
-
4
votes3
answers106
viewsA: How to convert HTML comments to HTML code?
It is generally good practice to use <template> to put HTML code on the page, this code is not enabled or rendered but can be more easily used or activated. In old code or where this is not…
-
2
votes2
answers49
viewsA: How to create a code snippet dynamically in Javascript?
I suggest you add a div around each group of inputs, so you can easily select a group. Then you can activate a function when the button is pressed and in that function you create a clone/copy with…
javascriptanswered Sergio 133,294 -
2
votes1
answer82
viewsA: How to pick variable that is in method and put in date ? Vue
Rewrites the datasets within the method listar. That is, if the initial dataset is datasets: [ { data: [this.categoriaC, this.categoriaD, this.categoriaE], backgroundColor: ["Red","Yellow","Purple"]…
-
0
votes2
answers59
viewsA: Add Account in Array and View information in a Table
You have some errors in your code: missing a } in function showAccounts to insert HTML you must use .innerHTML = 'string', and not .innerHTML('string'), That would be more like jQuery to insert…
-
0
votes1
answer191
viewsA: Test of default in Angulajs application using Karma and Jasmine
Things I see missing: the tests must know when they are complete When you have it('Teste AbreLink()...', function () {, and since you are doing asynchronous testing, you must return a Promise or use…
-
2
votes2
answers43
viewsA: How do I show a select according to the previous option?
This is a suggestion without jQuery. The idea is to use the value of select as references and hide the div.row when the value is indivudual. function toggleUser(type) { const userSelect =…
-
5
votes2
answers2705
viewsA: How to store an input value in a variable and show it
You can fetch the value of each input with form.querySelector(`[name="${name}"]`).value and then to create a string with these values you can do for example…
-
0
votes2
answers195
viewsA: How to remove class from other elements by clicking the button?
You can simplify and listen to events directly in the container: containers.forEach(item => { item.addEventListener('click', (e) => { item.querySelector('.popup').classList.toggle("visible");…
-
1
votes2
answers69
viewsA: enable option in select
If you have the value you can use a selector to find it and remove the attribute with removeAttr. A possible selector would be option[value="2"]... For example: $('#selectChannel…
-
3
votes1
answer60
viewsA: Merge 2 objects with Javascript ES6!
I leave a suggestion: maps the array items uses the array of headers and the .reduce to generate an object with the text to headers and the key value of the item named value of the header. Example:…
-
4
votes1
answer96
viewsA: Javascript - Dynamically HTML elements
Creating elements dynamically is very common. Not bad practice, on the contrary. What can be problematic is if these elements are not removed properly and remain in memory causing performance…
-
0
votes3
answers597
viewsA: Summing up JSON values
You have to treat that string so it can be converted to Number, then you have to add the values. An example would be: const dados = { success: [{ cod: "158877", txt: "158877", fields: { Valor:…