Javascript随机赞美生成器 [英] Javascript Random Compliment Generator

查看:141
本文介绍了Javascript随机赞美生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JS中,我正在尝试使用Arrays创建一个随机的赞美生成器。目标是当程序运行时,它从每个数组中取一个随机部分,然后显示字符串。我确定我在这里使用了太多变量,但我很挣扎。

页面顶部的变量(whatList,descriptionList和betterthanList)是我得到的,剩下的就是我尝试解决它。



var whatList = [hair,face,figure,brain,音乐品味,Javascript代码编写能力] ;

var descriptionList = [漂亮,聪明,美丽,精明,喜欢品尝彩虹];

var betterthanList = [在阿尔加维两个人的周末,Nooblab的金牌,保罗·内维的秃头,在学生会的地铁中吃了一顿饭,


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随机赞美生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆