I am very new to the Javascript and I am wondering how I can get the values from a function.
From my given code I would like to get the values of TheName, TheHeight, and TheGender, TheSexuality when I run the function so that I can use them for other purposes in my program.
Code:
function prng ()
{
var Name = Object.values(person.Names);
var Height = Object.values(person.Heights);
var Gender = Object.values(person.Genders);
var Sexuality = Object.values(person.Sexualities);
var TheName = Name[Math.floor(Math.random() * Name.length)];
var TheHeight = Height[Math.floor(Math.random() * Height.length)];
var TheGender = Gender[Math.floor(Math.random() * Gender.length)];
var TheSexuality = Sexuality[Math.floor(Math.random() * Sexuality.length)];
console.log('Name: ' + TheName + ', Height: ' + TheHeight +
', Gender: ' + TheGender + ', Sexuality: ' + TheSexuality);
}
Any help would be greatly appreciated. :)
you write something like that:
function nameYourFunction () { ... instead of console.log(...) you write: return 'Name: ' + TheName +...+ TheSexuality;
}
or you add all varibles to one object like that: https://www.w3schools.com/js/js_objects.asp
you can define object inline and return it like this
function prng ()
{
var person = {Names:'xx' , Heights:123,Genders:'male',Sexualities:'',};
var Name = Object.values(person.Names);
var Height = person.Heights;
var Gender = Object.values(person.Genders);
var Sexuality = Object.values(person.Sexualities);
var TheName = Name[Math.floor(Math.random() * Name.length)];
var TheHeight = Height[Math.floor(Math.random() * Height.length)];
var TheGender = Gender[Math.floor(Math.random() * Gender.length)];
var TheSexuality = Sexuality[Math.floor(Math.random() * Sexuality.length)];
return {Name : TheName ,Height: TheHeight, Gender:TheGender,Sexuality: TheSexuality};
}
var v = prng();
console.log(v);