Sometimes I do a code kata at codewars.com. That is a fun way to solve computer science related problems, learn on the way to solve them and especi...
For further actions, you may consider blocking this person and/or reporting abuse
Don't forget that while you can't redeclare variables using let, you can reassign them.
If you want a var that can not be reassigned, use const instead of let.
Just interesting, we have such example
let set = {};
edges.filter(e => e[0][1] !== e[0][0]).forEach(e => { if (!set[e[0]] || minOrMaxFunc(set[e[0]], e[1])>00) { set[e[0]] = e[1]; } });
let's say all the records match filter, in this case we will loop through all the edges twice?
Maybe using filter with forEach not the best example, what do you think about that?
Why do you think it will loop twice?
Context: input in this case is a weighted graph like this [["AB", 4], ["BC", 8], ["AC", 5]]. A, B, C are nodes, numbers are weights.
It is just a way to initialize a data structure. filter is used just to avoid "saving" edges like ["AA", 8] in object "set".
It could loop twice, let's say we have 1 billion of records.
All that records, as example has prop like, isFilterable = true
let's say we have a filter
edges.filter(e => e.isFilterable).forEach(e => { // some logic });
in this case we will loop through all the data twice.
It would be easier to use such a condition in the foreEach directly.
In this case you'll avoid duplicate actions on data.
Yes, filter pretty cool thing, but not in this case where we have filter().foreach()
Now I see your point. The advantage I see in this case is that a seperate "filter" step represents a seperate step in the algorithm. Like "step 1: filter out all edges that connect only one node. step 2: ...". If you use the filter condition in the forEach directly it gets more entangled with the next step.
Thanks for your comment.
I really don't understand this part of code. Is something missing?
Well, "t" is either "min" or "max". And edges is an array of 2 element arrays, e.g.
sort takes a function of two parameters that returns a number indicating whether the first parameter is smaller or bigger than the second parameter. The parameters are elements of the array. In our case these are 2 element arrays. In the code example, these are destructured as edge_a (e.g. "AB") and a (e.g. 3) respectively edge_b and b. The array edges is being sorted by the second element of the 2 element arrays.
Hope this helps.
Ah OK. I didn't understand that
t
was a variable previously set to"min"
or"max"
. Maybe it would be clearer to precise it, o add a line of code to show it.Thanks for your article, btw.
I deleted the const because it doesn't contribute to the point I want to make. It's much clearer now. Thanks for your comment.
extra [] on this example:
let [[a,b]] = 'ABC';
should be just
let [a,b] = 'ABC';
Fixed, thanks. Was working with nested arrays in this Kara so I got used to [[a,b]] :-)