Your XOR example is utterly banjaxed because it doesn't return the same thing; not even close. To the point I wonder if you understand binary.

Why?

[1,2,5] original returns 1, yours returns 6

0b0000 xor 0b0001 == 0b0001
0b0001 xor 0b0010 == 0b0011
0b0101 xor 0b0011 == 0b0110
0b0110 == 6 decimal.

Trust me, I used to hand assemble my own machine language. Other examples of where it fails:

[3, 3] original returns undefined, yours returns 0
[0, 0] original returns undefined, yours returns 0
[0, 1] original returns 0, yours returns 1
[0, 1, 2] original returns 0, yours returns 3
[1, 2, 3] original returns 1, yours returns 0

You get the idea. NOT that that original code is particularly well written with the blocks for nothing, LET/CONST crap (not a fan) for nothing, lack of safety short-circuit, lack of loop short circuiting, variables for nothing doing continue’s job, lack of normal every day “store the length first” optimization…

function betterSingleNumber(nums) {
var numLen = nums.length;
if (numLen < 2) return nums[0];
next: for (
var i = 0, value;
value = nums[i], i < numLen;
i++
) {
for (
var j = 0;
j == i ? j++ : 0, j < numLen;
j++
) if (value == nums[j]) continue next;
return value;
}
} // betterSingleNumber

Being a much better approach.

Side note, don’t let the pedantic fools unqualified to write a single line of code scare you away from continue/break labels. They’re talking out their backsides. The same can be said about the alleged “superiority” of let/const in most normal coding cases.

Could be worse though, that original could have used the derpy, cryptic, and overhead inducing foreach / arrow function garbage.

Your ternary operators section a LOT of developers would hold up as a reason not to over-use ternary due to the lack of code clarity, to which I’d say “format the bloody thing!!!”

let numString = (
num == 1 ? 'uno' : (
num == 2 ? 'two' : (
num == 3 ? 'three' : (
num == 4 ? 'four' : (
num == 5 ? 'five' : (
num == 6 ? 'six' : (
num == 7 ? 'seven' : (
num == 8 ? 'eight' : (
num == 9 ? 'nine' :
'zero'
))))))));

Though if ever there was a candidate to use an array lookup instead…

The intent of your “truthy” section would be good, if you didn’t have an “else” after the return. I’d force the length to boolean and short-circuit out as doing a premature return is a good thing.

if (!string.length) return;
// THEN do your processing

Also means you don’t introduce an extra wasteful block space, a concern now that those dumbass let/const are introducing extra overhead. Again, NOT a fan.

Your point about using functions for redundant code is a good one, BUT be careful with it. Every extra function you introduce adds overhead, AND code complexity as you end up having to hunt to find the function instead of seeing what’s being done right then and there. I’m not saying don’t do it, but beware of when it is and is not appropriate.

As to your Arrow functions section? Yeah, screw that needlessly pointlessly aggravatingly cryptic “wah wah, eye dunz wunna tiep teh wurd funkton” TLDR twitter generation idiocy. Particularly in the cases where it’s encouraging the use of anonymous functions for NOTHING.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store