DEV Community

5 things I learnt during my latest Javascript Code Kata

Erik Pischel on August 30, 2018

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...
Collapse
 
ingusmat profile image
Ingus Mat Burleson

Don't forget that while you can't redeclare variables using let, you can reassign them.

let a = 42;
console.log(a); // 42
let a = 24; // SyntaxError: redeclaration of let a
a = 24;
console.log(a) // 24

If you want a var that can not be reassigned, use const instead of let.

const c = 43;
console.log(c) // 43
c = 51; // TypeError: invalid assignment to const `c'
Collapse
 
vshyrokov profile image
Viktor Shyrokov • Edited

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?

Collapse
 
erikpischel profile image
Erik Pischel

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".

Collapse
 
vshyrokov profile image
Viktor Shyrokov

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()

Thread Thread
 
erikpischel profile image
Erik Pischel

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.

Collapse
 
severo profile image
Sylvain Lesage

I really don't understand this part of code. Is something missing?

// sort edges by weight (min/max)
const asc = t === "min";
edges.sort(([edge_a, a], [edge_b, b]) => asc ? a - b : b - a);
Collapse
 
erikpischel profile image
Erik Pischel

Well, "t" is either "min" or "max". And edges is an array of 2 element arrays, e.g.

edge=[["AB",3], ["BC",9], ["AC",5]]

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.

Collapse
 
severo profile image
Sylvain Lesage • Edited

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.

Thread Thread
 
erikpischel profile image
Erik Pischel

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.

Collapse
 
acthp profile image
acthp

extra [] on this example:

let [[a,b]] = 'ABC';

should be just

let [a,b] = 'ABC';

Collapse
 
erikpischel profile image
Erik Pischel

Fixed, thanks. Was working with nested arrays in this Kara so I got used to [[a,b]] :-)