[英]math.random always returning 1 in array
所以我運行代碼大約 50 次,每次它返回 olg / 1 所以出了什么問題
alert("welcome to word unscrambler undscramble this")
console.log("i am not a distraction")
document.write("i am not a distraction either")
var r = ["pttoao","recme","logd","teey","olg"]
var g=Math.floor(Math.random() *+ 6);
if (g=="0") {select =("pttoao")}
else if(g=="1"){select=("recme")}
else if (g==2){select="logd"}
else if(g==3){select="dre"}
else if(g==4){select="olg"}
if(select=="pttoao"){realword="potato"}
if(select=="recme"){realword="creme"}
if(select=="logd"){realword="gold"}
if(select=="teey"){realword="yeet"}
if(select="olg"){realword="log"}
var awnser= prompt("unscramble "+select)
if(awnser==realword){alert("correct")
}else{
alert("incorrect")}
也許是它不能隨機 select 字
這是一些適用於任意數量單詞的代碼。
注意結構:對象數組,對象有真實的單詞和它的混亂。
random
是相對於字數的。
我已將提示、檢查和警報合並為一行。
“通過復雜化來簡化......”:)
var words=[ {real:"potato", scrambled:"pttoao"}, {real:"creme", scrambled:"recme"}, {real:"gold", scrambled:"logd"}, {real:"yeet", scrambled:"teey"}, {real:"log", scrambled:"olg"} ]; var random=Math.floor(Math.random()*words.length); alert( prompt("Unscramble: "+words[random].scrambled) == words[random].real? "Correct": "Wrong" );
var g=Math.floor(Math.random() *+ 6 )
應替換為以下數組元素索引從零開始,數組“r”包含 5 個元素
var g=Math.floor(Math.random() * 5);
if(select="olg"){realword="log"}
在這里,您已將“olg”值分配給 select 變量。 在“if”條件賦值中只是返回賦值為“olg”的值,使其為真並執行
realword="log"
它應該替換為
if(select=="olg"){realword="log"}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.