Need assistance in understanding javascript order of precedence - javascript

I'm having an issue understanding how the following statement is working.
// setup
var n = 3;
var J = 3;
var g = 0;
var p = 41;
var m = false;
var O = 22;
var z = 15;
var I = [7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,21,19,20,22,18,20,19,21,7,7,17,17,17,17,17,17,17,17,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,25,25,25,25,25,25,25,25,7,7,29,27,28,30,26,28,27,29,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,,,22]
// statement
n += J || (g = p, m = p < O ? g - 3 : g + 2, I[m] < z | I[m + O - p] || I[p += p - O]) ? 1 : 0;
At the end of the statement n=4 but I don't understand how or why. I would expect:
n += 3 || 1 // n = 6
This is someone else's code I'm trying to understand. Any help is appreciated.

Let's simplify your expression a little.
n += X || Y ? 1 : 0
where
X = J
Y = (g = p, m = p < O ? g - 3 : g + 2, I[m] < z | I[m + O - p] || I[p += p - O])
Here we can see that we are evaluating X || Y and, on the basis of this, the ternary operator ? returns a value of either 1 or 0.
Since X || Y evaluates to true, we get n += 1 impliying that n = 4.

It's quite simple.
J || (g = p, m = p < O ? g - 3 : g + 2, I[m] < z | I[m + O - p] || I[p += p - O]) ? 1 : 0
This above equals (J || anything) ? 1 : 0. Since J=3, it's like (3 || anything) ? 1 : 0.
n += 3 || anything ? 1 : 0 -> n += 1

Related

Efficient way to get longest palindrome subsequence in a string

I'm trying to implement a function that takes a string as input and returns the longest palindrome subsequence in the string.
I've tried using dynamic programming and have come up with the following code:
function longestPalindromicSubsequence(str) {
let n = str.length;
let dp = Array(n);
for (let i = 0; i < n; i++) {
dp[i] = Array(n);
dp[i][i] = 1;
}
for (let cl = 2; cl <= n; cl++) {
for (let i = 0; i < n - cl + 1; i++) {
let j = i + cl - 1;
if (str[i] === str[j] && cl === 2)
dp[i][j] = 2;
else if (str[i] === str[j])
dp[i][j] = dp[i + 1][j - 1] + 2;
else
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
}
}
return dp[0][n - 1];
}
However, this code doesn't seem to be giving me efficient and better results for all test cases. The Time and Space Complexity is also be reduced. I've been struggling with this for days and can't seem to find the issue. Can someone help me figure out what's going wrong and how to fix it?
Oh, I think Dynamic Programming does not work with this sort of problem, because it does not break down recursively, i.e. to find the longest palindrome in a string, you don't need all second-largest palindromes. You can just check at each position and see if it is the center of a palindrome longer than any before. This can be solved with a greedy algorithm:
const pals = "asd1234321fghjkl1234567887654321qwertzu1234321"
function palindromeAtPos(str, pos, checkEven = false){
let ix = 0
const off = checkEven ? 2 : 1
while(pos-ix-1 >= 0 && pos+ix+1+off < str.length && str[pos-ix-1] === str[pos+ix+off]){
ix++
}
return ix === 0 ? str[pos] : str.substring(pos-ix, pos+ix+off)
}
function longestPalindrome(str){
let longest = ''
for(let i = 1; i < str.length; i++){
const odd = palindromeAtPos(str, i)
longest = odd.length > longest.length ? odd : longest
const even = palindromeAtPos(str, i, true)
longest = even.length > longest.length ? even : longest
}
return longest
}
console.log(longestPalindrome(pals))
On paper (and for a string like aaaaaaaaaa), this has quadratic complexity, but for most strings, it will be almost linear.
/*
* s => string
* return [] of strings witch have the max lenth
*/
function maxLenPalindromes(s) {
const l = s.length
let c, z, zz, a, b, a1, b1, maxl = 0, result = []
if (l < 2) return result
for (c = 0; c < l - 1; c++) {
a = -1
if (maxl>(l-c)*2+1) return result
if (c > 0 && s[c - 1] == s[c + 1]) {
zz = Math.min(c, l - c - 1)
for (z = 1; z <= zz; z++) {
if (s[c - z] != s[c + z]) {
a = c - z + 1; b = c + z
break
}
else if (z == zz) {
a = c - z; b = c + z + 1
break
}
}
if (a >= 0) {
if (b-a > maxl) {
result = [s.slice(a, b)]
maxl = b-a
}
else if (b-a == maxl) {
result.push(s.slice(a, b))
}
}
}
a=-1
if (s[c] == s[c + 1]) {
if (c == 0 || c == l - 2) {
a = c; b = c + 2
}
else {
zz = Math.min(c, l - c - 2)
for (z = 1; z <= zz; z++) {
if (s[c - z] != s[c + z + 1]) {
a = c - z + 1; b = c + z + 1
break
}
else if (z == zz) {
a = c - z; b = c + z + 2
break
}
}
}
if (a >= 0) {
if (b-a > maxl) {
result = [s.slice(a, b)]
maxl = b-a
}
else if (b-a == maxl) {
result.push(s.slice(a, b))
}
}
}
}
return result
}
const s1="112233111222333"
const s2="11_22_33_111_222_333"
const s3="12345_54321xqazws_swzaq_qwertytrewq"
const s4="sdfgsdfg1qqqqqAAAAA_123456789o o987654321_AAAAAqqqqq;lakdjvbafgfhfhfghfh"
console.log(maxLenPalindromes(s1))
console.log(maxLenPalindromes(s2))
console.log(maxLenPalindromes(s3))
console.log(maxLenPalindromes(s4))

How to understand Ternary JavaScript expression?

I'm bad with JS at now, especially with the operator "?"
And I'm trying to understand the following code.
Maybe it could be more friendly ?
So, if I don't want to use this operator, how can it be looks.
JavaScript code:
function(t) {
for (var e, r = t.length, n = "", i = 0, s = 0, a = 0; i < r; )
(s = t.charCodeAt(i)) < 128 ? (n += String.fromCharCode(s),
i++) : s > 191 && s < 224 ? (a = t.charCodeAt(i + 1),
n += String.fromCharCode((31 & s) << 6 | 63 & a),
i += 2) : (a = t.charCodeAt(i + 1),
e = t.charCodeAt(i + 2),
n += String.fromCharCode((15 & s) << 12 | (63 & a) << 6 | 63 & e),
i += 3);
return n
}
It seems like this is same as:
function(t) {
for (var e, r = t.length, n = "", i = 0, s = 0, a = 0; i < r; )
if((s = t.charCodeAt(i)) < 128) {
n += String.fromCharCode(s);
i++;
} else if(s > 191 && s < 224) {
a = t.charCodeAt(i + 1);
n += String.fromCharCode((31 & s) << 6 | 63 & a);
i += 2;
} else {
a = t.charCodeAt(i + 1);
e = t.charCodeAt(i + 2);
n += String.fromCharCode((15 & s) << 12 | (63 & a) << 6 |
63 & e);
i += 3;
}
return n
}
This is an overly complicated expression involving multiple ternary operators.
I think it should be simplified.
A ternary operator behaves like an if but it is an expression that returns one value out of 2 options, depending on the first operand.
For example:
operand ? valueIfTrue : valueIfFalse is a ternary expression that returns valueIfTrue if operand is "truthy" and returns valueIfFalse if operand is "falsey".
You can substitute any expression in place of valueIfTrue and valueIfFalse and this way you can get really complicated expressions, sometimes unnecessarily complex.
As an example of making expressions complicated, let's consider: For example: operand ? valueIfTrue : valueIfFalse
If we then replace valueIfTrue with another ternary operator, e.g. myOtherOperand ? myOtherIfTrue : myOtherIfFalse then the original expression becomes:
operand ? myOtherOperand ? myOtherIfTrue : myOtherIfFalse: valueIfFalse
This is not a nice way to write it, it can be improved like this, I just put parenthesis.
operand ? (myOtherOperand ? myOtherIfTrue : myOtherIfFalse) : valueIfFalse
It can be improved again by formatting like this:
operand
? myOtherOperand
? myOtherIfTrue // if both operand and myOtherOperand are true
: myOtherIfFalse // if operand is true and myOtherOperand is false
: valueIfFalse // this will be returned if operand is false
This shows that code formatting is essential for understanding it. But of course the first step is to have simple code. Anyways, here is how I would format the code in the question so it can be easier to understand:
function myFunction(t) {
for (var e, r = t.length, n = "", i = 0, s = 0, a = 0; i < r; ) {
(s = t.charCodeAt(i)) < 128
? (n += String.fromCharCode(s), i++)
: s > 191 && s < 224
? (a = t.charCodeAt(i + 1), n += String.fromCharCode((31 & s) << 6 | 63 & a), i += 2)
: (a = t.charCodeAt(i + 1),
e = t.charCodeAt(i + 2),
n += String.fromCharCode((15 & s) << 12 | (63 & a) << 6 | 63 & e),
i += 3); // end of ternary operators
return n;
}
}
Now it is clearer and we see statements separated by commas inside of the two ternary operators that are used. Commas are used to execute multiple things in the same expression, e.g. (n += String.fromCharCode(s), i++) will increase n and also i. In this case, it is better to move those outside of a ternary and into a normal if statement like this:
function myFunction(t) {
for (var e, r = t.length, n = "", i = 0, s = 0, a = 0; i < r;) {
const firstCheck = (s = t.charCodeAt(i)) < 128;
const secondCheck = s > 191 && s < 224;
if (firstCheck) {
n += String.fromCharCode(s);
i++;
} else if (secondCheck) {
// This is originally: (a = t.charCodeAt(i + 1), n += String.fromCharCode((31 & s) << 6 | 63 & a), i += 2);
a = t.charCodeAt(i + 1);
n += String.fromCharCode((31 & s) << 6 | 63 & a);
i += 2;
} else {
// this is originally:
// (a = t.charCodeAt(i + 1),
// e = t.charCodeAt(i + 2),
// n += String.fromCharCode((15 & s) << 12 | (63 & a) << 6 | 63 & e),
// i += 3);
a = t.charCodeAt(i + 1);
e = t.charCodeAt(i + 2);
n += String.fromCharCode((15 & s) << 12 | (63 & a) << 6 | 63 & e);
i += 3;
}
return n;
}
}
So basically break it down and take it step by step to understand it, then you can change it because you understand it.

Decompression Routine In Javascript to Python

I'm trying to rewrite a javascript file decompression routine to python. But I can't seem to get it right. The function always returns None on python.
Here is the original JS:
var c = new Uint8Array(b),
d = 0,
e = new Uint32Array(4096),
f = new Uint32Array(4096),
g = 256,
h = a.length,
k = 0,
l = 1,
m = 0,
n = 1;
c[d++] = a[0];
for (var r = 1;; r++) {
n = r + (r >> 1);
if (n + 1 >= h) break;
var m = a[n + 1],
n = a[n],
p = r & 1 ? m << 4 | n >> 4 : (m & 15) << 8 | n;
if (p < g)
if (256 > p) m = d, n = 1, c[d++] = p;
else
for (var m = d, n = f[p], p = e[p], q = p + n; p < q;) c[d++] = c[p++];
else if (p == g) {
m = d;
n = l + 1;
p = k;
for (q = k + l; p < q;) c[d++] = c[p++];
c[d++] = c[k]
} else break;
e[g] = k;
f[g++] = l + 1;
k = m;
l = n;
g = 4096 <= g ? 256 : g
}
return d == b ? c : null
And here is my python implementation, what did I do wrong? (Improved with #le_m answer but still returning None.)
c = [0] * b
d = 0
e = [0] * 4096
f = [0] * 4096
g = 256
h = len(a)
k = 0
l = 1
m = 0
n = 1
c[d] = a[0]
d += 1
r = 1
while True:
n = r + (r >> 1)
if (n + 1) >= h:
break
m = a[n + 1]
n = a[n]
p = (m << 4 | n >> 4) if r & 1 else ((m & 15) << 8 | n)
if (p < g):
if (256 > p):
m = d
n = 1
c[d] = p
d += 1
else:
m = d
n = f[p]
p = e[p]
q = p + n
while p < q:
c[d] = c[p]
d += 1
p += 1
elif p == g:
m = d
n = 1 + 1
p = k
q = k + l
while p < q:
c[d] = c[p]
d += 1
p += 1
else:
break
e[g] = k
f[g] = l + 1
g += 1
k = m
l = n
g = 256 if 4096 <= g else g
r += 1
Just from glancing at the code, I spot the following differences:
new Uint8Array(b) translates to [0] * b as the typed array is initialized with zeros. Same holds for the other typed arrays.
c[d++] = a[0] translates to c[d] = a[0] followed by d += 1, not the other way round. Same holds for the other post-increments, too.
The loop counter r += 1 should go to the end of the loop body or should be initialized with 0 instead.
I recommend stepping through both implementations step by step with a debugger (your browser's developer console for JavaScript) and compare the variable values.

Most effecient way to solve a formula in javascript

Say I have the following formula
a + (13 * (b/c)) + d + (12 * e) - f - 11 + (g * (h/i)) - 10 = 66;
Now the letters a-i can represent a number from 1-9. However, each number can only be used once. What would be the most efficient way to solve this? The way I have at the moment is to generate an array of random numbers e.g.
array[2,5,1,7,8,6,9,3,4]
I then fit this into the formula to check if it equals 66. If not, I generate a new array of numbers. I was thinking there should be a better way to do this though, maybe using recursion?
Just wanted to get some input in how you would tackle this?
Thanks
I think there's no smarter algorithm than just trying with each permutation of the numbers in the set for equality. Which I think will be easier for you, than creating everytime an unique array, that was not already generated.
In the code bellow I have used very primitive check for equality between numbers. There might be an easier way from the standard javascript library (e.g. if number is found in the predefined set...)
var solutions = [];
for (var a = 1; a <= 9; a++) {
for (var b = 1; b <= 9; b++) {
if ( b == a ) continue;
for (var c = 1; c <= 9; c++) {
if ( c == b || c == a ) continue;
for (var d = 1; d <= 9; d++) {
if ( d == c || d == b || d == a ) continue;
for (var e = 1; e <= 9; e++) {
if ( e == d || e == c || e == b || e == a ) continue;
for (var f = 1; f <= 9; f++) {
if ( f == e || f == d || f == c || f == b || f == a ) continue;
for (var g = 1; g <= 9; g++) {
if ( g == f || g == e || g == d || g == c || g == b || g == a ) continue;
for (var h = 1; h <= 9; h++) {
if ( h == g || h == f || h == e || h == d || h == c || h == b || h == a ) continue;
for (var i = 1; i <= 9; i++) {
if ( i == h || i == g || i == f || i == e || i == d || i == c || i == b || i == a ) continue;
if ((a + (13 * (b/c)) + d + (12 * e) - f - 11 + (g * (h/i)) - 10) == 66) {
solutions.push(a + "," + b + "," + c + "," + d + "," + e + "," + f + "," + g + "," + h + "," + i);
}
}
}
}
}
}
}
}
}
}
console.log(solutions);
Which results into a lot of solutions
["1,2,6,4,7,8,3,5,9", "1,2,6,4,7,8,5,3,9", "1,3,2,4,5,8,7,9,6", "1,3,2,4,5,8,9,7,6", "1,3,2,9,5,6,4,7,8", "1,3,2,9,5,6,7,4,8", "1,3,4,7,6,5,2,9,8", "1,3,4,7,6,5,9,2,8", "1,3,6,2,7,9,4,5,8", "1,3,6,2,7,9,5,4,8", "1,3,9,4,7,8,2,5,6", "1,3,9,4,7,8,5,2,6", "1,4,8,2,7,9,3,5,6", "1,4,8,2,7,9,5,3,6", "1,5,2,3,4,8,7,9,6", "1,5,2,3,4,8,9,7,6", "1,5,2,8,4,7,3,9,6", "1,5,2,8,4,7,9,3,6", "1,5,3,9,4,2,7,8,6", "1,5,3,9,4,2,8,7,6", "1,9,6,4,5,8,3,7,2", "1,9,6,4,5,8,7,3,2", "1,9,6,7,5,2,3,4,8", "1,9,6,7,5,2,4,3,8", "2,1,4,3,7,9,5,6,8", "2,1,4,3,7,9,6,5,8", "2,3,6,1,7,9,4,5,8", "2,3,6,1,7,9,5,4,8", "2,4,8,1,7,9,3,5,6", "2,4,8,1,7,9,5,3,6", "2,8,6,9,4,1,5,7,3", "2,8,6,9,4,1,7,5,3", "2,9,6,3,5,1,4,7,8", "2,9,6,3,5,1,7,4,8", "3,1,4,2,7,9,5,6,8", "3,1,4,2,7,9,6,5,8", "3,2,1,5,4,7,8,9,6", "3,2,1,5,4,7,9,8,6", "3,2,4,8,5,1,7,9,6", "3,2,4,8,5,1,9,7,6", "3,2,8,6,5,1,7,9,4", "3,2,8,6,5,1,9,7,4", "3,5,2,1,4,8,7,9,6", "3,5,2,1,4,8,9,7,6", "3,6,4,9,5,8,1,7,2", "3,6,4,9,5,8,7,1,2", "3,9,2,8,1,5,6,7,4", "3,9,2,8,1,5,7,6,4", "3,9,6,2,5,1,4,7,8", "3,9,6,2,5,1,7,4,8", "4,2,6,1,7,8,3,5,9", "4,2,6,1,7,8,5,3,9", "4,3,2,1,5,8,7,9,6", "4,3,2,1,5,8,9,7,6", "4,3,9,1,7,8,2,5,6", "4,3,9,1,7,8,5,2,6", "4,9,6,1,5,8,3,7,2", "4,9,6,1,5,8,7,3,2", "5,1,2,9,6,7,3,4,8", "5,1,2,9,6,7,4,3,8", "5,2,1,3,4,7,8,9,6", "5,2,1,3,4,7,9,8,6", "5,3,1,7,2,6,8,9,4", "5,3,1,7,2,6,9,8,4", "5,4,1,9,2,7,3,8,6", "5,4,1,9,2,7,8,3,6", "5,4,8,9,6,7,1,3,2", "5,4,8,9,6,7,3,1,2", "5,7,2,8,3,9,1,6,4", "5,7,2,8,3,9,6,1,4", "5,9,3,6,2,1,7,8,4", "5,9,3,6,2,1,8,7,4", "6,2,8,3,5,1,7,9,4", "6,2,8,3,5,1,9,7,4", "6,3,1,9,2,5,7,8,4", "6,3,1,9,2,5,8,7,4", "6,9,3,5,2,1,7,8,4", "6,9,3,5,2,1,8,7,4", "7,1,4,9,6,5,2,3,8", "7,1,4,9,6,5,3,2,8", "7,2,8,9,6,5,1,3,4", "7,2,8,9,6,5,3,1,4", "7,3,1,5,2,6,8,9,4", "7,3,1,5,2,6,9,8,4", "7,3,2,8,5,9,1,6,4", "7,3,2,8,5,9,6,1,4", "7,3,4,1,6,5,2,9,8", "7,3,4,1,6,5,9,2,8", "7,5,2,8,4,9,1,3,6", "7,5,2,8,4,9,3,1,6", "7,6,4,8,5,9,1,3,2", "7,6,4,8,5,9,3,1,2", "7,9,6,1,5,2,3,4,8", "7,9,6,1,5,2,4,3,8", "8,2,4,3,5,1,7,9,6", "8,2,4,3,5,1,9,7,6", "8,3,2,7,5,9,1,6,4", "8,3,2,7,5,9,6,1,4", "8,5,2,1,4,7,3,9,6", "8,5,2,1,4,7,9,3,6", "8,5,2,7,4,9,1,3,6", "8,5,2,7,4,9,3,1,6", "8,6,4,7,5,9,1,3,2", "8,6,4,7,5,9,3,1,2", "8,7,2,5,3,9,1,6,4", "8,7,2,5,3,9,6,1,4", "8,9,2,3,1,5,6,7,4", "8,9,2,3,1,5,7,6,4", "9,1,2,5,6,7,3,4,8", "9,1,2,5,6,7,4,3,8", "9,1,4,7,6,5,2,3,8", "9,1,4,7,6,5,3,2,8", "9,2,8,7,6,5,1,3,4", "9,2,8,7,6,5,3,1,4", "9,3,1,6,2,5,7,8,4", "9,3,1,6,2,5,8,7,4", "9,3,2,1,5,6,4,7,8", "9,3,2,1,5,6,7,4,8", "9,4,1,5,2,7,3,8,6", "9,4,1,5,2,7,8,3,6", "9,4,8,5,6,7,1,3,2", "9,4,8,5,6,7,3,1,2", "9,5,3,1,4,2,7,8,6", "9,5,3,1,4,2,8,7,6", "9,6,4,3,5,8,1,7,2", "9,6,4,3,5,8,7,1,2", "9,8,6,2,4,1,5,7,3", "9,8,6,2,4,1,7,5,3"]
If the formula is only this, and will not change, you might try to think if there are ranges that are not included in the result set per definition. For example, if you find that some of the numbers cannot be 9, you should not iterate to 9. E.g. the multiplications in the formula might exceed a number that even added to the minimal values in the other variables to exceed 66.
Brute force method:
function findSolution() {
for (var a = 1; a <= 9; a++) {
for (var b = 1; b <= 9; b++) {
for (var c = 1; c <= 9; c++) {
for (var d = 1; d <= 9; d++) {
for (var e = 1; e <= 9; e++) {
for (var f = 1; f <= 9; f++) {
for (var g = 1; g <= 9; g++) {
for (var h = 1; h <= 9; h++) {
for (var i = 1; i <= 9; i++) {
if (
[b,c,d,e,f,g,h,i].indexOf(a) >= 0 ||
[a,c,d,e,f,g,h,i].indexOf(b) >= 0 ||
[a,b,d,e,f,g,h,i].indexOf(c) >= 0 ||
[a,b,c,e,f,g,h,i].indexOf(d) >= 0 ||
[a,b,c,d,f,g,h,i].indexOf(e) >= 0 ||
[a,b,c,d,e,g,h,i].indexOf(f) >= 0 ||
[a,b,c,d,e,f,h,i].indexOf(g) >= 0 ||
[a,b,c,d,e,f,g,i].indexOf(h) >= 0 ||
[a,b,c,d,e,f,g,h].indexOf(i) >= 0
) {
continue;
}
if (a + (13 * (b/c)) + d + (12 * e) - f - 11 + (g * (h/i)) - 10 === 66) {
return [a,b,c,d,e,f,g,h,i];
}
}
}
}
}
}
}
}
}
}
}
document.getElementById('result').innerText = findSolution().join(', ')
<span id="result"></span>

Strange use of "for" cycle in Javascript, please explain

I found this strange JavaScript I cannot understand. The for cycle has a strange syntax (many parameters), can you explain me how it is intended to work? Thanks
decode: function(s){
for(var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
((a = s[i][c](0)) & 0x80) &&
(s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
);
return s.join("");
}
That's an ordinary for loop, but with a very long var statement in the first part.
It's just like
var a, b, c;
Also the iterator statement in the for loop contains a lot of operations instead of the loop actually having a body.
Either this function was written by a terrible programmer with no regard for readable code, or it has been intentionally minified and obfuscated.
interesting function, apparently trans-coding a certain set of chars, kind of esoteric and will only work with an ASCII code but here's the breakdown:
for (var i = 0; i < s.length; i++) {
var a = s.charCodeAt(i);
if (a & 0x80) { // (a >= 128) if extended ascii
var b = s.charCodeAt(i + 1);
var specialA = (a & 0xfc) === 0xc0; // a IS [À, Á, Â or Ã] essentially [192, 193, 194, 195]
var specialB = (b & 0xc0) === 0x80; // b >= 128 & b <= 191 eg. b is not a special Latin Ascii Letter
if (specialA && specialB) {
var txA = (a & 0x03) << 6; // [0, 64, 128, 192]
var txB = b & 0x3f; // 0 - 63
s[i] = String.fromCharCode(txA + txB);
} else {
s[i] = String.fromCharCode(128);
s[++i] = "";
}
}
}
hope this helps, either way i found the decoding interesting, reminds of reading raw assembler, lol -ck
The different parts of the for loop is all there, divided by the semicolons (;).
The var part:
var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt";
The check part:
++i < l;
The update part:
((a = s[i][c](0)) & 0x80) &&
(s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
After the for() statement comes a ; right away, meaning that the loop doesn't have a body, but all the statements in the var-, check-, and update part will still be executed untill the check is no longer true.
Looks like someone didn't want their code to be readable. Where did you find it, anyway?
Breaking the loop into a one more readable:
rearranged loop parameters
changed (...)&&(...) with an if(...){(...)}
changed l to len
moved s = s.split(...) outside the len
.
var a, b, s = s.split(""), o = String.fromCharCode, c = "charCodeAt";
for(var i = -1, len = s.length; ++i < len;){
if((a = s[i][c](0)) & 0x80){
(s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ? o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "");
}
}
changed i initial value and how/where it increases
moved a = s[i][c](0) outside
.
var a, b, s = s.split(""), o = String.fromCharCode, c = "charCodeAt";
for(var i = 0, len = s.length; i < len; i++){
a = s[i][c](0);
if(a & 0x80){
s[i] = (a & 0xfc);
(s[i] == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ? o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "");
}
}
created tmp to make things easier to read
stored the ternary operation result in tmp
splitted (s[i] == 0xc0 && tmp, s[++i] = ""); with an
if(...){s[++i] = "";}
replaced the new loop inside the your example
.
decode: function(s){
var tmp, a, b, s = s.split(""), o = String.fromCharCode, c = "charCodeAt";
for(var i = 0, len = s.length; i < len; i++){
a = s[i][c](0);
if(a & 0x80){
s[i] = (a & 0xfc);
if(((b = s[i + 1][c](0)) & 0xc0) == 0x80){
tmp = o(((a & 0x03) << 6) + (b & 0x3f));
}else{
tmp = o(128);
}
if(s[i] == 0xc0 && tmp){
s[++i] = "";
}
}
}
return s.join("");
}
Final result /\

Categories