var random1 = Math.random()* 7; var randoma = Math.floor(random1); var random2 = Math.random()* 6; var randomb = Math.floor(random2); var random3 =数学。 random()* 5; var randomc = Math.floor(random3); var whatChoice1; var whatChoice2; var whatChoice3; var whatChoice4; var whatChoice5; var whatChoice6; var desChoice1; var desChoice2; var desChoice3; var d esChoice4; var desChoice5; var betterChoice1; var betterChoice2; var betterChoice3; var betterChoice4; if(randoma = 0) { whatChoice1 = whatList [0]; } else if(randoma = 1) { whatChoice2 = whatList [1]; } else if(randoma = 2) { whatChoice3 = whatList [2]; } else if(randoma = 3) { whatChoice4 = whatList [3]; } 否则if(randoma = 4) { whatChoice5 = whatList [4]; } else if(randoma = 5) { whatChoice6 = whatList [5]; } if(randomb = 0) { desChoice1 = descriptionList [0]; } else if(randomb = 1) { desChoice2 = descriptionList [1]; } else if(randomb = 2) { desChoice3 = descriptionList [2]; } else if(randomb = 3) { desChoice4 = descriptionList [3]; } else if(randomb = 4) { desChoice5 = descriptionList [4]; } if(randomc = 0) { betterChoice1 = betterthanList [0]; } else if(randomc = 1) { betterChoice2 = betterthanList [1]; } else if(randomc = 2) { betterChoice3 = betterthanList [2]; } 否则(randomc = 3) { betterChoice4 = betterthanList [3]; } console.log(你的+ whatChoice [] +是+ desChoice [] +并且优于+ betterChoice []); 解决方案 为什么不直接使用变量? if语句不是必需的。 您可能只想检查randoma / randomb的范围是否在数组的下边界和上边界: whatChoice3 = whatList [randoma]; desChoice2 = descriptionList [randomb]; 祝你好运! 你不需要if链:你有数组! 例如 whatList 中的选择可以用这种方式编码: var rwl = Math.floor((Math.random()* 6)); // 生成介于0..5(包含)之间的随机数 var whatChoice = whatList [rwl]; // 使用随机数索引数组。 In JS, I''m trying to make a random compliment generator using Arrays. The goal is that when the program is run, it takes a random part from each array and then displays the string. I''m sure i''ve used far too many variables here, but I am struggling.The variables at the top of the page (whatList, descriptionList, and betterthanList) are what i''ve been given, and the rest is my attempt to solve it.var whatList = ["hair", "face", "figure", "brain", "taste in music", "ability to code in Javascript"];var descriptionList = ["pretty", "intelligent", "beautiful", "astute", "like tasting a rainbow"];var betterthanList = ["a weekend for two in the Algarve", "a gold medal on Nooblab", "the baldness of Paul Neve", "a slap up meal in Subway in the student union"];var random1 = Math.random()*7;var randoma = Math.floor(random1);var random2 = Math.random()*6;var randomb = Math.floor(random2);var random3 = Math.random()*5;var randomc = Math.floor(random3);var whatChoice1;var whatChoice2;var whatChoice3;var whatChoice4;var whatChoice5;var whatChoice6;var desChoice1;var desChoice2;var desChoice3;var desChoice4;var desChoice5;var betterChoice1;var betterChoice2;var betterChoice3;var betterChoice4;if(randoma = 0){ whatChoice1 = whatList[0];}else if (randoma = 1){ whatChoice2 = whatList[1];}else if(randoma = 2){ whatChoice3 = whatList[2];}else if(randoma = 3){ whatChoice4 = whatList[3];}else if(randoma = 4){ whatChoice5 = whatList[4];}else if(randoma = 5){ whatChoice6 = whatList[5];}if (randomb = 0){ desChoice1 = descriptionList[0];}else if (randomb = 1){ desChoice2 = descriptionList[1];}else if(randomb = 2){ desChoice3 = descriptionList[2];}else if(randomb = 3){ desChoice4 = descriptionList[3];}else if(randomb = 4){ desChoice5 = descriptionList[4];} if(randomc = 0){ betterChoice1 = betterthanList[0];}else if (randomc = 1){ betterChoice2 = betterthanList[1];}else if(randomc = 2){ betterChoice3 = betterthanList[2];}else if(randomc = 3){ betterChoice4 = betterthanList[3];}console.log("Your " + whatChoice[] + " is" + desChoice[] + " and better than" + betterChoice[]); 解决方案 Why not use the variables directly? The if statements aren''t necessary.You might just want to check if the range of randoma/randomb is in the lower and upper boundary of the array:whatChoice3 = whatList[randoma];desChoice2 = descriptionList[randomb];Good luck!You don''t need the if chains: you have arrays!For instance the choiche in whatList could be coded this way:var rwl = Math.floor((Math.random()*6)); // generate a random number between 0..5 (included)var whatChoice = whatList[rwl]; // use the random number to index the array. 这篇关于Javascript随机赞美生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!