Reputation: 187
Can you please look at this function and tell me where the error is? Firebug says that "string is undefined" ... Any help is appreciated. (links is declared above, console.debug(string) shows a comma delimited string)
function adRotate() {
var id = Math.floor(Math.random()*links.length);
var string = links[id];
var item = string.split(',');
console.debug(item);
}
Upvotes: 1
Views: 6799
Reputation: 664777
The code should work. If the console "shows a comma delimited string", this should either be a string or an Array.
In case adRotate(["one,link", "second,link,"])
- links is an Array
- you'd get:
function adRotate(links) {
var id = Math.floor(Math.random()*links.length); // valid index in links
var str = links[id]; // selects one of the links: str is a String
var item = str.split(','); // splits the string to an Array
console.debug(item); // logs the array
}
possible results: ["one","link"]
or ["second","link"]
In case adRotate("one link, and second")
- links is a String
- you'd get:
function adRotate(links) {
var id = Math.floor(Math.random()*links.length); // valid index in links
var str = links[id]; // selects one of the chars in the string: str is a String of length 1
var item = str.split(','); // splits the one-char-string to an Array
console.debug(item); // logs the array
}
possible results: ["o"]
, ["n"]
, ["e"]
, [" "]
, ..., ["k"]
, ["",""]
(for the comma char), ["i"]
, etc.
Upvotes: 1