sort()uses alphanumeric sort per default.
[1,2,5,10].sort() will output
[1, 10, 2, 5].
To properly sort an array, you can use
[1,2,5,10].sort((a, b) => a — b)
Easy solution, provided you knew there was a problem in the first place :)
new Date() can accept:
x: returns 1st of january 1970, + x milliseconds. Unix people know why.
I find it to be a good thing, as I don’t like functions that mutate their input. You also should know that
replace will only replace the first match:
If you wish to replace all occurences, you can use a regex with
"bob".replace(/b/g, 'l') === 'lol' // replace all occurences
Reason: [1,2,3] and [1,2,3] are two separate arrays. They just happen to contain the same values. They have distinct references and cannot be compared with
To know if your var is an array, you can still use
Did you expect it to output 0, 1, 2… ? Do you know why it does not ? How would you fix it ?
Let’s mention two of the possible solutions to this problem:
var. Boom. solved.
“The difference [between let and var] is scoping.
varis scoped to the nearest function block and
letis scoped to the nearest enclosing block (both are global if outside any block), which can be smaller than a function block.” (source)
There are plenty of other ways to do this. These are only my top-2 choices :)
What do you think this will output ?
One point for you if you think this will crash with
Cannot read property 'name' of undefined
greet is not run with proper context. Again, there are many ways to solve this.
This way you ensure that
greet is called with your class instance as context.
greetshould never be run out of context, you can also bind it in your class constructor:
=>) can be used to preserve the context. This will also work:
although I find this last example less elegant in our case.
Congrats, you’re now able to put stuff on the internet. Probably. Perhaps even without breaking everything (generally it does, though). Cheers \o/
Let me know if there’s anything I should have mentioned!