/    Sign up×
Community /Pin to ProfileBookmark

I’m missing somthing!

Hey guys,

Can someone take a quick look at this and tell me what’s wrong?

It creates a random code, like ‘poi4gjs’. Once to code is there, I pass it to a php script I wrote for machine unreadable image verification ( you know the ones ).

I keep getting an error on line 22, which is my first array.

I was under the impression that Javscript 1.2, you can set the array directly.

[CODE]
function randCode(){
//create random 7 character code with 1 number in the middle
var arrCode = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’,’k’,’l’,’m’,’n’,’o’,’p’,’q’,’r’,’s’,’t’,’u’,’v’,’w’,’x’,’y’,’z’];
var arrNums = [0,1,2,3,4,5,6,7,8,9];
for(i=0; i < 7; i++){
var t = Math.floor(Math.random()*arrCode.length);
var tcode += arrCode[t];
};

t = Math.floor(Math.random()*arrNums.length);
//add a random number to the middle of the code
var code = tcode.substr(0,3) + arrNums[t] + tcode.substr(4);

return code;
};
[/CODE]

thanks

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterJan 24.2006 — <i>
</i>function randCode(){
//create random 7 character code with 1 number in the middle
var arrCode = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y', 'z'];
var arrNums = [0,1,2,3,4,5,6,7,8,9];
for(var i=0,t,tcode=""; i &lt; 7; i++){
t = Math.floor(Math.random()*arrCode.length);
tcode += arrCode[t];
};

<i> </i>t = Math.floor(Math.random()*arrNums.length);
<i> </i>//add a random number to the middle of the code
<i> </i>var code = tcode.substr(0,3) + arrNums[t] + tcode.substr(4);

<i> </i>return code;
};
Copy linkTweet thisAlerts:
@CrazyMerlinauthorJan 24.2006 — bugger! lmao!

I was resetting tcode each time wasn't I?!

Thanks Ultimater!
Copy linkTweet thisAlerts:
@UltimaterJan 24.2006 — Unlike PHP, in JavaScript, an undefined variable doesn't yield a value of ""

Even if we used your original code, found and changed the statement:
<i>
</i>var tcode += arrCode[t];

into:
<i>
</i>tcode += arrCode[t];

and initiate tcode as follows before the for-loop:
<i>
</i>var tcode;

Your string would contain the word "undefined" within it because in JavaScript the addition opperator is used for both string concentration and the addition of numbers.

PHP, on the otherhand knows beforehand that the variable is a string and sets it set to an empty string.

So you'd need to initiate tcode as follows, using an empty string (so JavaScript doesn't think it is a number)
<i>
</i>var tcode="";



Example before setting tcode to an empty string:
<i>
</i>&lt;script type="text/javascript"&gt;
function randCode(){
//create random 7 character code with 1 number in the middle
var arrCode = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y', 'z'];
var arrNums = [0,1,2,3,4,5,6,7,8,9];
for(var i=0,tcode; i &lt; 7; i++){
var t = Math.floor(Math.random()*arrCode.length);
tcode += arrCode[t];
};

<i> </i>t = Math.floor(Math.random()*arrNums.length);
<i> </i>//add a random number to the middle of the code
<i> </i>var code = tcode.substr(0,3) + arrNums[t] + tcode.substr(4);

<i> </i>return code;
};
alert(randCode())
&lt;/script&gt;


An example after setting tcode to an empty string:

<i>
</i>&lt;script type="text/javascript"&gt;
function randCode(){
//create random 7 character code with 1 number in the middle
var arrCode = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y', 'z'];
var arrNums = [0,1,2,3,4,5,6,7,8,9];
for(var i=0,[color=blue]tcode=""[/color]; i &lt; 7; i++){
var t = Math.floor(Math.random()*arrCode.length);
tcode += arrCode[t];
};

<i> </i>t = Math.floor(Math.random()*arrNums.length);
<i> </i>//add a random number to the middle of the code
<i> </i>var code = tcode.substr(0,3) + arrNums[t] + tcode.substr(4);

<i> </i>return code;
};
alert(randCode())
&lt;/script&gt;




Also, take note that in JavaScript variables not defined with "var" are global unlike PHP that variables are local by default unless you specify initiate the variable with the "global" method of PHP. So in JavaScript you would want to initiate the variable "i" like so: [b]var i;[/b] but in PHP, all variables within a function are local by default so you don't run into this issue. Get used to initiating local variables with "var" within JavaScript functions so you aren't defining gloabl variables unwilling like so:


Before using "var":
<i>
</i>&lt;script type="text/javascript"&gt;
function globalVariable(){
i="I hope this variable isn't global!";
};

var i="3.1415926535897932384626"
globalVariable()
alert(i)
&lt;/script&gt;


After using "var":
<i>
</i>&lt;script type="text/javascript"&gt;
function globalVariable(){
var i="I hope this variable isn't global!";
};

var i="3.1415926535897932384626"
globalVariable()
alert(i)
&lt;/script&gt;



I hope this clears things up for you and many others, you're welcome.
×

Success!

Help @CrazyMerlin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...