Note: I have read https://meta.stackexchange.com/questions/128548/what-stack-overflow-is-not/129362#129362
It says "try to write the code yourself and post what you've tried when you run into problems." which is what I am doing here.
The original PHP code:
$text = "Hello world, I am rainbow text!";
$texty = '';
$colors = array('ff00ff','ff00cc','ff0099','ff0066','ff0033','ff0000',
'ff3300','ff6600','ff9900','ffcc00','ffff00','ccff00',
'99ff00','66ff00','33ff00','00ff00','00ff33','00ff66',
'00ff99','00ffcc','00ffff','00ccff','0099ff','0066ff',
'0033ff','0000ff','3300ff','6600ff','9900ff','cc00ff');
$i = 0;
$textlength = strlen($text);
while($i<=$textlength){
foreach($colors as $key=>$value){
if (isset($text[$i])) {
$texty .= "<font color=\"#".$value."\">".$text[$i]."</font>";
}
$i++;
}
$texty = str_replace("> <","> <",$texty);
echo $texty;
}
What I've butchered it down to:
var text = "Hello world, I am rainbow text!";
var texty = '';
colors = new Array
('ff00ff','ff00cc','ff0099','ff0066','ff0033','ff0000',
'ff3300','ff6600','ff9900','ffcc00','ffff00','ccff00',
'99ff00','66ff00','33ff00','00ff00','00ff33','00ff66',
'00ff99','00ffcc','00ffff','00ccff','0099ff','0066ff',
'0033ff','0000ff','3300ff','6600ff','9900ff','cc00ff');
var i = 0;
var textlength = text.length;
var key = '';
var value = '';
while(i <= textlength){
for each(colors as key=>value){
if (text[i] != undefined) {
texty .= "<font color=\"#" + value + "\">" + text[i] + "</font>";
}
i++;
}
texty.replace("> <","> <");
//document.write(texty);
}
I've been testing this as Javascript, which is why I have document.write commented in the code. However, I still cannot get it to work. I hate to be so vague but... can someone tell me where I screwed up?
Got something with your code, but didnt get what you trying to do. Check out fiddle.
http://jsfiddle.net/ymutlu/pKCcS/
this looks better...
posted code here, in case i delete fiddle link.
var text = "Hello world, I am rainbow text!";
var texty = '';
colors = ['ff00ff','ff3300','ff6600','ffff66','00ff99','00ffcc','00ffff','00ccff','0099ff','0066ff','0033ff','0000ff','3300ff','6600ff','9900ff','cc00ff'];
var i = 0;
var textlength = text.length;
var key = '';
var value = '';
while(i <= textlength){
var t = text.charAt(i);
if (t!= undefined) {
texty += "<font color=\"#" + colors[i%colors.length] + "\">" + t + "</font>";
i++;
}
}
texty.replace("> <","> <");
document.write(texty);
Actionscript is basically a dialect of Javascript, so you can test your code in an interactive shell in Firebug or Chrome's developer tools. There you'll get error reports.
Looking at your code, I can immediately spot a few errors, there might be others too:
for each(colors as key=>value){
This is not a valid construct. Write as:
for (var key in colors) {
var value = colors[key];
This is not valid syntax:
texty .=
Use:
texty +=
This is valid, but doesn't do what you expect:
texty.replace("> <","> <");
You need to assign the return value:
texty = texty.replace("> <","> <");
There's probably more ...