想要练习干编码;你能看看吗?

  • 本文关键字:练习 编码 javascript
  • 更新时间 :
  • 英文 :


我现在正在学习JavaScript,并且知道我正在使用此脚本中的功能重复自己。

我正在从事的项目是一个随机的幻想角色。

http://dapperdesign.me/charactercreator/

我的.js文件中有多个功能,具有相似的代码块。

我拥有的是:

//sample variable
const charClass = ['mage', 'warrior', 'rogue', 'paladin', 'wizard'];
//sample function
function getRandomClass() {
   var x = charClass[Math.floor(Math.random() * charClass.length)];
   return x;
}

我想要的是这个(从现在开始弥补东西(:

function getRandom(anything){
var x = anything[Math.floor(Math.random() * anything.length)];
       return x;
}

因此,当我以后打电话时,我可以使用我在程序开头设置的任何属性(即班级,头发,年龄等。(。

谢谢大家!

完整的程序我已经有以下内容:

//Establish some things
let gender = ['Male', 'Female'];
const firstMaleName = ['Win', 'Hakhad', 'Brirstirk', 'Olcerth', 'Benon',
'Lor', 'Meh', 'Fan', 'Tie', 'Arth', 'Stonnor', 'Razzik', 'Rungag', 'Maven', 'Corrin',
'Brother', 'Mason', 'Mannon', 'Brick', 'Atlas', 'Harry'];
const firstFemaleName = ['Naris','Cloud', 'Laflo', 'Wie', 'Rasal', 'Losho', 'Clara',
'Mara', 'Leia', 'Sunie', 'Elisabette', 'Mae', 'Willa', 'Gregina', 'Marry',
'Jessica', 'Sarah', 'Buff', 'Tate', 'Melody'];
const lastName = ['Dorilbes', 'Biv', 'Hia', 'Doomtail', 'Skullsher', 'Thuly', 'Gallagher',
'Riddon', 'Bont', 'Sercy', 'Lightfoot', 'Took', 'Baggins', 'Maple', 'Dickle', 'Slimer',
'Vinkman', 'Drake']
const hairColor = ['red', 'blonde', 'silver', 'white', 'black'];
const charClass = ['mage', 'warrior', 'rogue', 'paladin', 'wizard'];
const charMood = ['stoic', 'goofy', 'violent', 'complacent', 'silly', 'cautious', 'brave'];

//Decide gender
let myGender = gender[Math.floor(Math.random() * gender.length)];
function storyGender(){
  if (myGender === 'Male'){
    return 'man';
  } else {
    return 'woman';
  }
}

//Choose first name based on gender
function chooseName(){
if (myGender === 'Male'){
  var x = firstMaleName[Math.floor(Math.random() * firstMaleName.length)];
  return x;
} else { var y = firstFemaleName[Math.floor(Math.random() * firstFemaleName.length)];
return y;
}
}
//How old are you?
function age(){
let x = Math.floor(Math.random() * 100) + 1;
return x;
}
//Pick random last name (regardless of gender)
function getLastName() {
var x = lastName[Math.floor(Math.random() * lastName.length)];
return x;
}
//Pick random hair color
function getRandomHair() {
var x = hairColor[Math.floor(Math.random() * hairColor.length)];
return x;
}
//Pick a random class
function getRandomClass() {
var x = charClass[Math.floor(Math.random() * charClass.length)];
return x;
}
//Your disposition
function getMood() {
var x = charMood[Math.floor(Math.random() * charMood.length)];
return x;
}
//Test things
function testing(){
var msg = 'You are ' + chooseName() + ' ' + getLastName() + '. A ' + getMood() + ' ' + getRandomClass() + ' ' + 'who is ' + age() + ' years old, from a land far, far away.';
msg += ' Everyone notices your ' + getRandomHair() + ' hair as you stroll into the Inn. Your entrance captures the eyes of a shadowy figure in the room.';
msg += ' The shadow says, "Ah yes, just the ' + storyGender() + ' I was looking for..."';
return msg;
}
function reload() {
    location.reload();
}

尝试研究开关语句,如果允许您执行某个条件,则有效地执行代码块。

在您的示例中,您需要一个功能来采用参数

function getRandom(attribute)

然后,使用Switch语句检查属性是什么,并根据以下方式执行一个代码块:

switch(attribute)
{
    case attribute == "class":
       var x = charClass[Math.floor(Math.random() * charClass.length)];
       return x;
       break;
}

并重复您需要的任何东西。

最新更新