2015-08-17 101 views
0

我已經開始進行溝通,並且試圖製作一個代碼,它會爲它生成單詞,並按照一組規則放置字母。我想讓它把所有的字母放到一個數組中,然後加入數組來創建一個單詞。問題是,我無法讓它改變數組。有人可以看一下代碼來找出問題所在?這裏有一個JSFiddle(抱歉,如果我的代碼很混亂)。雖然循環幫助字發生器

https://jsfiddle.net/rxgab975/

var words = []; 
var consonants = ["B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "P", "R", "S", "T", "V", "Z"]; 
var consonantPairsBeg = ["SP","ST","SK","SL","SM","FP","FT","FK","FL","FM","CP","CT","CK","CL","CM","KL","KR","GL","GR","VL","JT","JL","JK"]; 
var consonantPairsEnd = ["RS","RP","RT","RK","RL","RM","RG","RV","PS","TS","KS","LS","MS","GS","VS","LP","LT","LK","LM","LG","LV","PC","TC","KC","LC","MC","GC","VC"]; 
var vowels = ["A", "E", "I", "O", "U", "AA", "AI", "AO", "II"]; 
var order = "(B)V(E)(C)F"; 
var wordcount = 1000; 

function pickLet(type) { 
var chance = Math.random(); 
if (type === "B") { 
    if (chance >= 0.5) { 
     return consonantPairsBeg[Math.floor((Math.random() * consonantPairsBeg.length))]; 
    } else { 
     return consonants[Math.floor((Math.random() * consonants.length))]; 
    } 
} else if (type === "V") { 
    return vowels[Math.floor((Math.random() * vowels.length))]; 
} else if (type === "E") { 
      if (chance >= 0.5) { 
     return consonantPairsEnd[Math.floor((Math.random() * consonantPairsEnd.length))]; 
    } else { 
     return consonants[Math.floor((Math.random() * consonants.length))]; 
    } 
} else if (type === "C") { 
    return consonants[Math.floor((Math.random() * consonants.length))]; 
} 
}; 
function genWord() { 
var sylFin = false; 
var wordPos = 0; 
var partCount = 0; 
var chance = 0; 
var prob = Math.random(); 
var parts = []; 
while (wordFin = false) { 
    if (order[wordPos] === "(") { 
     chance = Math.random(); 
     wordPos++; 
    } else if (order[wordPos] === ")") { 
     chance = 0; 
     wordPos++; 
    } else if(order[wordPos] === "B") { 
     if (prob > chance) { 
      parts[partCount] = pickLet("B"); 
      partCount++; 
     } 
     wordPos++; 
    } else if(order[wordPos] === "V") { 
     if (prob > chance) { 
      parts[partCount] = pickLet("V"); 
      partCount++; 
     } 
     wordPos++; 
    } else if(order[wordPos] === "E") { 
     if (prob > chance) { 
      parts[partCount] = pickLet("E"); 
      partCount++; 
     } 
     wordPos++; 
    } else if(order[wordPos] === "C") { 
     if (prob > chance) { 
      parts[partCount] = pickLet("C"); 
      partCount++; 
     } 
     wordPos++; 
    } else if(order[wordPos] === "F") { 
     sylFin = true; 
    } 
} 
return parts; 
}; 

回答

0

出於藍: 1-糾正genWord(條件語句),如下:

While (wordFin == false) { 

2-可取的做法是使用var關鍵字之前wordFin聲明使用它。我沒有看到你的機制來停止循環,你在哪裏利用邏輯中的wordcount來停止循環?

希望這會有所幫助。

+1

那麼修復它。我想我應該刪除這篇文章,因爲我後來在代碼中錯誤地命名了這個變量,這就是導致這個問題的原因。我想我只是需要一個沒有寫代碼的人:P – Xelipho