2013-08-28 152 views
1

開始學習JavaScript,所以我決定製作一個程序,該程序會接收一個字符串,然後返回相同的字符串,但每個字符的顏色都不相同。Javascript函數未定義

不知道爲什麼當我編譯時,當我打電話給我的彩虹功能時,函數沒有定義。 此外,試圖打印出字符串,但我不知道如果我正確地做。任何合乎邏輯和風格的建議和編輯非常感謝!

<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> 
+0

你應該做的'clr'值的字符串。 –

+0

@ DanielA.White我不知道該怎麼做。你的意思是像'#33FFFF'? – Liondancer

+1

你也犯了一個錯字:'mystring.lenth'應該是'mystring.length'。 – mc10

回答

4

所有的顏色應該是一個字符串替換它。

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; 
    } 
1

你在哪裏叫它?因爲我看到它不應該給你未定義。

,你做錯了

rainbowstring[counter] = <span style = 'color:"+clr+"'>"+letter+"</span>; 

應該是:

rainbowstring[counter] = "<span style = 'color:"+clr+"'>"+letter+"</span>"; 

雖然更好的將rainbowstring作爲一個經常性的變量,因爲這樣的:

rainbowstring += "<span style = 'color:"+clr+"'>"+letter+"</span>"; 

,並在結束你應該寫

x.innerHTML = rainbowstring; 

即時消息還不能確定是否能使用「stringlength」考慮「strlength」或類似

+0

當我打電話給我的按鈕內的彩虹,我得到一個錯誤,說它是未定義的... – Liondancer

+0

@Liondancer請包括該代碼,可能是因爲你的腳本標記不是在你的身體元素末尾 –

+0

那段代碼是在底部我提供的代碼 – Liondancer

-1

隨着一點點的jQuery ..... jsFiddle

jQuery的

var rbow, ltrColor; 
var colors = new Array('00','33','66','99','CC','FF'); 
$('#rainbower').click(function() { 
    var blah = $('#rainbowinput').val(); 
    var lenBlah = blah.length; 
    rbow = ""; 
    for (c = 0; c < lenBlah; c++) { 
     l = blah.charAt(c); 
     ltrColor = "#"; 
     for (rgb = 0; rgb < 3; rgb++) { 
      rndColor = (Math.floor(Math.random() * 6)); 
      ltrColor += colors[rndColor]; 
     } 
     rbow += "<span style='color:" + ltrColor + "'>" + l + "</span>"; 
    } 
    $('#rainbowoutput').html(rbow); 
}); 

HTML

<form> 
    Enter String: 
    <input type="text" name="rainbowstring" id="rainbowinput" /> 
    <br /> 
</form> 
<button type="button" id="rainbower">Rainbow Generator</button> 
<div id="rainbowoutput"></div>