0

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("> <",">&nbsp;<",$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("> <",">&nbsp;<");
//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?

1
  • What happens when you try to run this as ActionScript? We can't say where the bugs are if we don't know what happens when it is run. Are you using this in a Flash or Flex project? Does it compile ok? Commented May 24, 2012 at 9:03

2 Answers 2

1

Got something with your code, but didnt get what you trying to do. Check out fiddle.

http://jsfiddle.net/ymutlu/pKCcS/

this looks better...

http://jsfiddle.net/pKCcS/2/

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("> <",">&nbsp;<");
document.write(texty);

Sign up to request clarification or add additional context in comments.

1 Comment

The second one worked wonders and fixed my duplicate string issue I was having, thanks!
1

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("> <",">&nbsp;<");

You need to assign the return value:

texty = texty.replace("> <",">&nbsp;<");

There's probably more ...

1 Comment

The for (var key in someArray) {} can sometimes give unexpected results because it can include elements higher up in the prototype chain. Better to get used to for (var i=0; i<someArray.length; i++){} construct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.