Posts by hkotsubo • 55,826 points
1,422 posts
-
3
votes2
answers146
viewsA: What is Function.prototype.call() for?
In Javascript, the this is very dynamic, varying according to the context. And call is one of the ways to control it. For example, if I have a function that returns this: function bla() { return…
-
1
votes2
answers365
viewsA: Javascript - How to add things to html and save html information
The basic idea is to use createElement to create the new fields and add them to an existing element. There are other details to tidy up, for example: one id should be unique on the page, but you…
javascriptanswered hkotsubo 55,826 -
3
votes2
answers787
viewsA: Regex that accepts only letters or letters and numbers
In my opinion, this problem is simpler to solve without regex, but anyway, we will see a solution with and another without, then you draw your own conclusions. The problem with her regex is that she…
-
0
votes2
answers87
viewsA: I do not understand the error None in this function
The function failed to return a value. In the end it should have a return final: def dnaComplement(s): complement = [] complement0 = reversed(s) for character in complement0: if character == 'G':…
-
1
votes3
answers392
viewsA: How to use reduce in an object array in React
I know the question is how to use reduce (and another answer already explained how to do it), but in fact you don’t need it. A loop simple already solves: let total = 0; for (const fruit of fruits)…
-
5
votes2
answers163
viewsA: Power in Java other than Excel
Kotlin is not "parenting," it’s just executing things in the order you indicated. What happens is that Math.pow is a method that takes two arguments: the base and the exponent - and they are…
-
1
votes2
answers53
viewsA: How can I associate typed and stored values in an array to a menu option?
I thought I’d try to solve it with a dictionary, but I’d have to type in the name of the operating system as a key every time I wanted to compute a vote. Actually no, with a dictionary you can use…
-
1
votes2
answers69
viewsA: OFF - Is it possible to print a list value by passing its index?
If you want exactly the texts "First", "Second", etc., there is no direct way - at least natively - to get it. The way is to build the texts yourself, or use an external module. If you choose to…
-
2
votes2
answers342
viewsA: Validate an expression based on which parentheses should close
If the idea is only check the parentheses (without taking into account if the rest is a valid arithmetic expression), you only need one counter, and only one loop running through the string…
-
1
votes2
answers69
viewsA: Difference of Month in Javascript
If you want the difference in months or years, do not need to calculate this way. The problem of months and years is that they do not have fixed sizes. A month may be 28, 29, 30 or 31 days, and a…
-
1
votes1
answer214
viewsA: Print Matrix (C)
You’re complicating the algorithm for nothing. Just make a for from 0 to 11 (because the indexes of an array start at zero). If the index is even, it means that it is the first of the line, then you…
-
2
votes2
answers117
viewsA: How to use while in javascript
The way you’re using do...while is wrong. The syntax is just: do { faz algo } while (condição); But you did: do { faz algo } while (condição) { faz outra coisa } However, this block after the while…
javascriptanswered hkotsubo 55,826 -
3
votes1
answer128
viewsA: Fix badly formatted JSON
The ideal solution is to fix JSON at source. Who sends the data must make sure they are in the correct format. Point. Any other solution (even more so with regex) will be a non-ideal (also known as…
-
0
votes1
answer54
viewsA: Map data like Mysql TIME to JAVA above 24 hours Example (37:26:30)
First we need to understand two important concepts: times and durations. Consider the sentences below: The meeting was at two o'clock in the afternoon The film is two hours long In the first case,…
-
0
votes1
answer42
viewsA: Taking the title of an HTML page with sed or awk
sed and awk (with regular expressions - regex) are not the most suitable tools for the task. Generally speaking, regex is not meant to work with HTML (may even "work" in many cases, but is not the…
-
3
votes2
answers70
viewsA: How to make calculations with instances of a class, or even with new instances?
As already said in another answer, Javascript does not support operator overload, so the way is to have a method in your class that sums the values and returns another object with the result: class…
javascriptanswered hkotsubo 55,826 -
2
votes1
answer46
viewsA: How to print/insert in an organized way inside a csv file a tuple in Python
According to the documentation of communicate the return is a tuple containing the standard output and error output. Also, when it is not in text mode, the return is an object of type bytes (can you…
-
2
votes2
answers102
viewsA: rotate a "for" loop with 5 variables together
In shell script, just make a loop by the letters of a up to the letter you want (let’s assume it’s c): for i in {a..c} do mesclar $i.* done This, assuming that in the directory you only have the…
-
1
votes3
answers136
viewsA: How to make calculations by clicking the button and placing the result in another field
The first thing I’d do is put a id in each input, so it becomes easier to find them. A another answer ended up using selectors as '#lbs + input', to get the input which comes just after the…
javascriptanswered hkotsubo 55,826 -
3
votes3
answers3811
viewsA: Unparseable date: "2017-10-30T02:00:00.000Z"
From Java 8 you can use the API java.time. In this case, the string "2017-10-30T02:00:00.000Z" represents a date and time in UTC (is what the "Z" at the end indicates, which is in UTC), and in…
-
2
votes1
answer80
views -
2
votes3
answers375
viewsA: What does [] mean after a variable?
Without a larger context regarding the code, the most we can say is: The let is one of the ways to declare a variable, and influences the scope of it - all of this has been explained in detail here.…
-
1
votes2
answers332
viewsA: How to change the color of only the clicked element?
An alternative (with limitations, we will already see below) is, whenever you click on a summary, look for if there is already a red (and if there is, change the color back to the original), and…
javascriptanswered hkotsubo 55,826 -
2
votes1
answer97
viewsA: Compare current time with file values
The time comparison you are doing will not work. When you do the Parsing of a string that only has the hour and the minute ('%H:%M'), seconds and fractions of a second are set to zero. In addition,…
-
4
votes2
answers311
viewsA: Regular expression to detect nested structures in a template
As already said in the comments, do not use regex, use a parser (or adapt an existing, or build a). Regex is not the right tool, especially when there are nested structures. Throughout the answer, I…
-
3
votes1
answer351
viewsA: Merge the contents of two lists by considering the contents in Python?
Assuming that the values always have only one digit, one option is to do the math - the numbers in the first list correspond to the ten, and those in the second list correspond to the unit, so just…
-
2
votes4
answers2358
viewsA: Return day of week from date in dd/mm/yyy format
From Java 8, you can use API java.time import java.time.DayOfWeek; import java.time.format.DateTimeFormatter; import java.time.format.TextStyle; import java.util.Locale; public static String…
-
3
votes1
answer128
viewsA: How can I improve this code?
Without knowing the requirements it is difficult to say what would be the "right" or more appropriate, but in general, it is possible to suggest some things. If you do the break when the id for…
-
1
votes4
answers2972
viewsA: Convert data to Timezone
In addition, there are some details that were not covered in the other answers. For example, the day of the week and month are in English ("Tue Apr"), and by default SimpleDateFormat uses the…
-
1
votes2
answers516
viewsA: Remove items from one list that are in another
If the idea (confirmed in comments) is to have the elements of proc that nay are in fat, then one option is: proc = [1, 2, 3, 4] fat = [3, 4, 5, 6] result = [] for p in proc: if p not in fat:…
-
3
votes1
answer705
viewsA: How to remove blanks
If you enter the site https://www.sunoresearch.com.br/acoes/itsa4 and inspect some of these values in the browser console, you will see that it uses the , which corresponds to the no-break…
-
2
votes2
answers322
viewsA: Why the function returns the Undefined value
The main problem in your code is the comparison of arrays. You should not compare arrays using ==, because the result is not quite what you expect. For example, if I compare [] with [], should be…
-
1
votes1
answer42
viewsA: Typeerror in Python, I need to modify a string
In doing: boardnumFrom = input('Qual fileira voce esta? ') The variable boardnumFrom will be a string, because that’s what input returns. Then, you pass this string to the function mover:…
-
2
votes1
answer45
viewsA: I couldn’t quite understand how this code works
If so, it makes it easier to understand? matrix = [[1, 2], [3, 4], [5, 6], [7, 8]] transpose = [] for i in range(2): linha = [] for row in matrix: linha.append(row[i]) transpose.append(linha) This…
-
1
votes2
answers390
viewsA: Receive an integer number in the input, calculate and print the sum of the odd digits of this number in the output
You almost right, just checking if the digit is odd: x = abs(int(input("Numero: "))) soma = 0 while x != 0: resto = x % 10 if resto % 2 != 0: # só adiciona à soma se for ímpar soma += resto x //= 10…
-
4
votes1
answer608
viewsA: Convert date from one format to another
The mistake is that the method format gets a Date, but you’re going through a String. And actually the conversion should not be done this way. If you have a String in one format and want to convert…
-
2
votes1
answer86
viewsA: Calculation of Age using Period
The problem is you did if (validarData()), and the condition of if must be a boolean. But you stated that the method validarData returns a Period, that is not a boolean. Another detail is that the…
-
3
votes2
answers66
viewsA: object1 += object2 is different object1= object1 + object2 in python(3.8)?
What happens is a combination of two different behaviors. The first is what is described here and here. Read the links for more details, but summarizing, the operator += modifies the list itself,…
-
3
votes2
answers206
viewsA: Function returning Undefined - Javascript
forEach serves to do something with each element of an array. This "something" is a callback: a function that is passed as an argument to forEach, and executed for each element of the array. The…
-
1
votes1
answer132
viewsA: Remove duplicate values in a list and print them
In your case, the simplest seems to be to use the method removeAll. First you read all the values of each of the files, and then do the "subtraction": // método auxiliar para ler todos os valores do…
-
1
votes1
answer1179
viewsA: Typeerror: '>' not supported between instances of 'tuple' and 'int'
According to the documentation, enumerate returns a sequence of tuples containing the index and its element. Then for index in enumerate(lista_nota) the variable index is a tuple, and so gives this…
-
1
votes2
answers486
viewsA: Conversion from comma to java point
To format monetary values, a good alternative is to use NumberFormat. Ex: NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));…
-
9
votes2
answers328
viewsA: Print multiple numbers with a separator between them, but do not put the separator after the last
An alternative is to use join: print('|'.join(str(n) for n in range(numero1, numero2 + 1) if n % 5 == 0)) Although it seems the same thing from another answer, has a difference. There was created a…
-
1
votes1
answer613
viewsA: Count how many occurrences of one number there are in another, recursively
Just for the record, recursion is not the most efficient way to solve this problem. That said, let’s go to the solution. One of the problems with your job is that I can call you that: verifica(num,…
-
4
votes1
answer517
viewsA: Calculation of age in Java
In the calculation, you are only taking into account the year, but you should also check if the person’s birthday has passed (if it has not passed, you have to subtract 1 from age). Thus: public…
-
1
votes2
answers608
viewsA: Regex to validate currency, fractionated, accepting negatives
You can simplify the criteria. If the first digit is zero, there can be no sign before, and after it can have only the comma, or the comma followed by 1 or 2 digits (the cases 0,0, 0,00, 0,01 until…
-
2
votes2
answers326
viewsA: Regex to capture links from sites specific to an HTML page
As has already been said here (and also here, here and mainly here - and in many other places around), regex is not the ideal tool to manipulate HTML (read each of the links to understand all the…
-
3
votes3
answers199
viewsA: Compare values of two arrays and generate a third with the unique javascript values
If you’re already using compatible environments Ecmascript 2015, an alternative is to use Set, who already owns good support of current browsers. The Set ensures that its elements will be unique…
javascriptanswered hkotsubo 55,826 -
2
votes1
answer60
viewsA: Array is only returning the last index
This is because the array has two keys called idExterno. Basically what you did was (omitting some data to make it simpler to view): [[ "idExterno" => "1", "pergunta" => "Pergunta 1",…
-
4
votes2
answers619
viewsA: Modifying a Python file without losing the current content
First, call str in str(linha[8:14]) is redundant and unnecessary as linha[8:14] already returns a string. In addition, in the example file you reported, this excerpt corresponds to "ANANA " (but…