Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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[]);
Posted

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!
 
Share this answer
 
Comments
dteece1 12-Apr-13 4:26am    
That made the code so much simpler to navigate, and i've pretty much nailed it now! Thank you!
E.F. Nijboer 12-Apr-13 5:14am    
Great I could help you out :-)
You don't need the if chains: you have arrays!
For instance the choiche in whatList could be coded this way:
JavaScript
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900