Starting to learn JavaScript so I decided to make a program that would take in a string and then return the same string but it will have different colors on each character.
Not sure why when I compile, the moment I call my Rainbow function, the function isn't defined. Also, trying to print out the string but I'm not sure if I'm doing it correctly. Any logical and stylistic advice and edits is much appreciated!
<script>
function Rainbow(x) {
var mystring = String(x); @* convert to string*@
var Stringlength = mystring.lenth; @* length fo string *@
var rainbowstring = new Array(Stringlength); @* create array of appropriate size*@
var counter = 0;
var clr, letter;
while (counter < Stringlength) {
letter = mystring.charAt(counter);
var randomnumber = Math.floor(Math.random() * 10); @* random number generator --> 11 means 0-10 *@
switch (randomnumber) {
case 0: clr = #FF0000; break;
case 1: clr = #00FF00; break;
case 2: clr = #0000FF; break;
case 3: clr = #FF00FF; break;
case 4: clr = #000000; break;
case 5: clr = #00FFFF; break;
case 6: clr = #33FFFF; break;
case 7: clr = #33FF00; break;
case 8: clr = #FFFF00; break;
case 9: clr = #FF66CC; break;
}
rainbowstring[counter] = <span style = 'color:"+clr+"'>"+letter+"</span>; @* assign color *@
counter++; @* increment *@
}
return rainbowstring;
}
@* need something that generates colors *@
@* assigns colors to text *@
</script>
<form>
Enter String: <input type ="text" name ="rainbowstring" id ="rainbowinput"/><br>
</form>
<button
type = "button" onclick = "Rainbow(document.getElementById('rainbowinput').value)" > Rainbow Generator
</button>
clrvalues a string.mystring.lenthshould bemystring.length.