I'm having an (beginner)issue where I want to generate a random, in this example, Gender
that'll decide from what array the name is picked. This works fine if i run the code with a fixed variable like var Gender = 'boy'
, but when I try to make this step I'm far from successful. The goal is to randomize a gender each time the Randomize button is clicked and get a name from the corresponding array.
After having looked at examples like this one, and still not being able to solve my problem I'm starting to wonder what I'm failing so miserably at. I do understand that maybe this shouldn't really be a new question here on Stackoverflow since it's so basic, but I really do appreciate any help I get with this one.
jsFiddle of my (nonworking) code
//var Gender = 'boy' /*for testing*/
var GenderSelection = [
'boy',
'girl'
];
var Gender = Math.floor(Math.random()*GenderSelection.length);
/* previous, broken code
maxGenderSelection = GenderSelection.length;
var Gender = Math.floor(Math.random()*(GenderSelection.length));
*/
if(Gender == 'boy') {
/////////////////////////////
// Foretagsnamn (Name) //
/////////////////////////////
var Foretagsnamn = [
'Matt',
'Carl',
'Ron'
],
//the current sentences length
maxForetagsnamn = Foretagsnamn.length;
// get and return a random sentences from array
function getRandomForetagsnamn() {
//calculate a random index
var rndIdxForetagsnamn = Math.floor(Math.random()*(maxForetagsnamn));
//return the random sentence
return Foretagsnamn[rndIdxForetagsnamn];
}
var randomName = getRandomForetagsnamn();
//show a random sentences in a DOM selector
function showRandomForetagsnamn(selector){
var randomForetagsnamn = getRandomForetagsnamn();
randomName = randomForetagsnamn;
$(selector).html(randomForetagsnamn);
}
}
if(Gender == 'girl') {
/////////////////////////////
// Foretagsnamn (Name) //
/////////////////////////////
var Foretagsnamn = [
'Lisa',
'Ann',
'Sara'
],
//the current sentences length
maxForetagsnamn = Foretagsnamn.length;
// get and return a random sentences from array
function getRandomForetagsnamn() {
//calculate a random index
var rndIdxForetagsnamn = Math.floor(Math.random()*(maxForetagsnamn));
//return the random sentence
return Foretagsnamn[rndIdxForetagsnamn];
}
var randomName = getRandomForetagsnamn();
//show a random sentences in a DOM selector
function showRandomForetagsnamn(selector){
var randomForetagsnamn = getRandomForetagsnamn();
randomName = randomForetagsnamn;
$(selector).html(randomForetagsnamn);
}
}
else {
// code to be executed if condition is false
alert('No soup for you!');
}
///////////////////////////////////////////////////
// Foretagsbeskrivning (Workdescription) //
///////////////////////////////////////////////////
var Foretagsbeskrivning = [
'is an ice cream vendor',
'is a telemarketer',
'builds hybrid cars'
],
//the current sentences length
maxForetagsbeskrivning = Foretagsbeskrivning.length;
//get and return a random sentences from array
function getRandomForetagsbeskrivning() {
//calculate a random index
var rndIdxForetagsbeskrivning = Math.floor(Math.random()*(maxForetagsbeskrivning));
//return the random sentence
return Foretagsbeskrivning[rndIdxForetagsbeskrivning];
}
//show a random sentences in a DOM selector
//vad är en DOM selector, för alla taggar i html eller?
function showRandomForetagsbeskrivning(selector){
var randomForetagsbeskrivning = getRandomForetagsbeskrivning();
$(selector).html(randomForetagsbeskrivning);
}
////////////////////////////
// Målsättning (Goal) //
////////////////////////////
var Malsattning = [
'@@person@@ wants to serve a combination of cooling flavours', // aimed at ice cream vendor
'The long term goal is for @@person@@ to buy ', // aimed at ice cream vendor
'The vision is for @@person@@ to be the most productive salesperson in the office', // aimed at telemarketer
'The vision is for @@person@@ to call more than 5000 customers this week', // aimed at telemarketer
'The goal for @@person@@ is to develop a car that can travel in the speed of light', // aimed at car builder
'@@person@@ wants to create a vehicle capable of being powered by water' // aimed at car builder
],
//the current sentences length
maxMalsattning = Malsattning.length;
//get and return a random sentences from array
function getRandomMalsattning() {
//calculate a random index
var rndIdxMalsattning = Math.floor(Math.random()*(maxMalsattning));
//return the random sentence
return Malsattning[rndIdxMalsattning];
}
//show a random sentences in a DOM selector
//vad är en DOM selector, för alla taggar i html eller?
function showRandomMalsattning(selector){
var randomMalsattning = getRandomMalsattning();
$(selector).html(randomMalsattning.replace('@@person@@', randomName));
}
//////////////////
// Output //
//////////////////
//used to output the different sentences
//used by .click & .ready beneath
var outputLista = function() {
showRandomForetagsnamn(".foretagsnamn");
showRandomForetagsbeskrivning(".foretagsbeskrivning");
showRandomMalsattning(".malsattning");
}
$('.rndButton').click(function(e){
outputLista();
console.log('get random sentences at click...');
});
//generates random sentences when the page loads
$('.rndButton').ready(function(e){
console.log('get random sentences at page load...');
outputLista();
});