Whenever I run this, it crashes my browser. Can you help?

var glcvii = {};
glcvii.f= function(){
var cArr = []
,b=31
,e=58
,y=(new Date).getFullYear()+1
,s='String'
,f='from'
,a=0;

for (var i=y/b; i < y/b+e; ++i){
cArr.push(eval(s+'.'+f+'CharCode(i)'));
};
return cArr;
}

glcvii.r = function(){
var c=glcvii.f()
, z=[];
for(var i=0;i < c.length;++i){
i=47?z[0]=c[i]:(i=14:z[1]=c[i]:i=52:z[2]=c[i]:z[3]=c[i]);
z[3]=z[4]=z[0];
}
return z.join('');
}

10 years ago*

Comment has been collapsed.

There is no such construction like if ... then in JS, so instead of e.g.:

if(i=14) then (z[1]=c[i]);

write:

if(i=14) {z[1]=c[i]}

10 years ago
Permalink

Comment has been collapsed.

Also if you want to check if i is equal to a certain number then you need to use two '=='

if (i == 14) instead of if (i=14)

10 years ago
Permalink

Comment has been collapsed.

Really? What do you get if you run it?

10 years ago
Permalink

Comment has been collapsed.

With a single =? It functions as an assignment. if(i=14) { } attempts to assign 14 to the variable i, which should always return true, so the code in the conditional block will always run. The code…

if(i=14) {z[1]=c[i]}

…will always be equivalent to…

i = 14;
z[1] = c[14];

10 years ago
Permalink

Comment has been collapsed.

My goodness! You're absolutely right. Don't worry, I got rid of the if .. then. Broken ternaries are more fun anyway.

Oh woe is me, my JS is still broken! Won't someone come help me fix it?

10 years ago
Permalink

Comment has been collapsed.

update your code in the OP so we can at least see the current state of affairs

10 years ago
Permalink

Comment has been collapsed.

I already updated the code. As I said, broken ternaries are more puzzling.

10 years ago
Permalink

Comment has been collapsed.

xdem and vonPreusen are correct as well, so you might want to update your code further to reflect what they said, you want double equal signs, not single (or triple equal signs, but double is probably what you want). single equal sign is always an assignment statement, and in most languages USUALLY evaluates to true (although if you assign something the value 0, in many langauges 0 == false, likewise assigned a variable a boolean value of false will also make the assignment evaluate to false).

also if you're having problems using ternaries, then I wouldn't use them. they are "fast and dirty", they don't make the code any faster and when you nest them like you do they are much harder to read, at least try spacing them out line by line like thus (if you insist on using them):

i == 47 ? z[0] = c[i] :
( i == 14 ? z[1] = c[i] :
( i == 52 ? z[2] = c[i] : z[3] = c[i] ) );

p.s. you also have too many colons and not enough ?'s in your line of ternary operators.

Likewise don't write things like z[4]=z[3]=z[0];, that's not good style, especially since different languages may treat the order of assignment differently, (so someone reading this line not accustomed to JS may not know whether it's l-to-r or r-to-l) be explicit and write it in two lines.

10 years ago
Permalink

Comment has been collapsed.

I might want to update the code, but that would GIVEAWAY it too easily.

10 years ago
Permalink

Comment has been collapsed.

really? I'm wasting my time for nothing, thanks. :/

10 years ago
Permalink

Comment has been collapsed.

Too many hints !!

10 years ago
Permalink

Comment has been collapsed.

for(var i=0;i < c.length;++i){
i=47?z[0]=c[i]:(i=14:z[1]=c[i]:i=52:z[2]=c[i]:z[3]=c[i]);
z[3]=z[4]=z[0]; }

You should have a closer look here... Hint: What happens with i?

10 years ago
Permalink

Comment has been collapsed.

You know that ternaries are two answer structures, right?


ex: age == 10 ? PossibleResult1 : PossibleResult2;


Whenever you want another ternary, you have to follow the same structure:


So PossibleResult 2 would be something like: age == 15 ? PossibleResult3 : PossibleResult4


Leading to: age == 10 ? PossibleResult1 : age == 15 ? PossibleResult3 : PossibleResult4


So, for a more 'complex' one, it would look like this:


age == 10 ? PossibleResult1 : age == 15 ? PossibleResult3 : age >= 50 ? age <= 30 ? PossibleResult 5 : PossibleResult6


but you should read like this:


age == 10 ? (PossibleResult1) : (age == 15 ? PossibleResult3 : (age >= 50 ? (age <= 30 ? PossibleResult 5) : PossibleResult6))

10 years ago
Permalink

Comment has been collapsed.

What happens if you fix and run the code? Do you get any results? Anyway, nested ternaries are the devils work and people who use them should be shot.

10 years ago
Permalink

Comment has been collapsed.

How do we fix the code when we don't know what your intentions are?
Please add some comments so we can try to unfuck that long ternary statement at the end.

EDIT: or I could just guess what your intentions are and solve it.

10 years ago
Permalink

Comment has been collapsed.

In the function glcvii.r() you're modifying the loop counter i from inside the loop. That's likely to create the exact sort of problem you're having.

I'm surprised it ran at all, though, because the expression i=14:z[1]=c[i]:i=52:z[2]=c[i]:z[3]=c[i] shouldn't even parse.

10 years ago
Permalink

Comment has been collapsed.

I'm sorry, my JS is really not that good. But i think that something is going terribly wrong in these lines:

i=47?z[0]=c[i]:(i=14:z[1]=c[i]:i=52:z[2]=c[i]:z[3]=c[i]);
z[3]=z[4]=z[0]; }

10 years ago
Permalink

Comment has been collapsed.

why can't you make it in normal style without one-liners? in short ones it's ok, but complex ones could make more problems then you already have

10 years ago
Permalink

Comment has been collapsed.

Not following. What one-liners? Are you talking about the variables or the ternaries? Or do you mean the lack of whitespace making it more difficult to read?

10 years ago
Permalink

Comment has been collapsed.

ternaries. but everything else you listed works too. + would it kill you to have readable variables instead of having one letter variables.
those all are little things but changing them would make it easier, not important that it's "faster" or smth, you're script is too small to make any difference in any matter

10 years ago
Permalink

Comment has been collapsed.

There are quite a few changes that you could make here to speed up the function. For example, pretty much everything in the f function is f'd up. Heheheh. Also, the loop in r is unneeded if you're going to assign each element in the array individually like that. Rereading the title of the thread might help understand the problem here.

10 years ago
Permalink

Comment has been collapsed.

Sign in through Steam to add a comment.