为什么函数A的返回值在函数B中的调用方式不同?

时间:2019-12-17 21:24:21

标签: javascript variables return

function rollClass() {

  // Select a physical or magical god as well as its class
  var classList = new Array();
  classList[0] = 0; // Physical Hunter
  classList[1] = 1; // Physical Warrior
  classList[2] = 2; // Physical Assassin
  classList[3] = 3; // Magical Mage
  classList[4] = 4; // Magical Guardian

  // Computer shit to select an option
  var ran = Math.random(); // Create a new random number to truly randomize it

  var i = Math.floor(classList.length * ran);

  // Write it onto the developer log:
  document.getElementById("dLog1").innerHTML = "The variable i in function rollClass() is: " + i;

  // Sends the variable classList[i] to B
  var a = classList[i];

  // Write onto dLog #3. Test to see what variabe "a" shows up as
  document.getElementById("dLog3").innerHTML = "The variable a in function rollClass() is: " + a;

  // Output classList[i]
  return a;
}

// Stating the check class function first. Doesn't make a difference.
function checkClass() {

  // Grab classList[i] (var a) from function rollClass();
  var r = rollClass();

  var dLog = "Class";

  // Write out what r is in checkClass(r)
  // Developer log 5
  document.getElementById("dLog5").innerHTML = "The r in checkClass(r) is " + r;

  // Determine if it is a physical or magical god as well as its class
  if (r == 0) {
    // This is a physical hunter
    var type = "Physical";
    var typeC = "Hunter";
    var dLog = "Hunter";
  } else if (r == 1) {
    // This is a physical warrior
    var type = "Physical";
    var typeC = "Warrior";
    var dLog = "Warrior";
  } else if (r == 2) {
    // This is a physical assassin
    var type = "Physical";
    var typeC = "Assassin";
    var dLog = "Assassin";
  } else if (r == 3) {
    // This is a magical mage
    var type = "Magical";
    var typeC = "Mage";
    var dLog = "Mage";
  } else {
    // Assume it's a magical guardian
    var type = "Magical";
    var typeC = "Guardian";
    var dLog = "Guardian";
  }

  // Developer log
  document.getElementById("dLog2").innerHTML = "The value for classList[i]: " + dLog;

  // Sets variables for transfer to another function
  var a = type;
  var b = typeC;

  // Developer log 7, show what var a and variable b are
  document.getElementById("dLog7").innerHTML = "Variable a: " + a + "<br>Variable b: " + b;

  return [a, b];
}

// ---------------------------------------------------------------------------------------------------------
// GOD ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------

// Function to determine god. Creates an array list and then determines the god.
// t = type
// c = class
function rollGod() {

  var array = checkClass();
  var c = array[1];

  // the list of gods

  // Determine what the computer chose
  if (c == "Hunter") {
    // Hunter
    // There are 19 hunters
    var god = new Array(); // Array for hunters
    god[0] = "Ah Muzen Cab";
    god[1] = "Anhur";
    god[2] = "Apollo";
    god[3] = "Artemis";
    god[4] = "Cernunnos";
    god[5] = "Chernobog";
    god[6] = "Chiron";
    god[7] = "Cupid";
    god[8] = "Hachiman";
    god[9] = "Heimdallr";
    god[10] = "Hou Yi";
    god[11] = "Izanami";
    god[12] = "Jing Wei";
    god[13] = "Medusa";
    god[14] = "Neith";
    god[15] = "Rama";
    god[16] = "Skadi";
    god[17] = "Ullr";
    god[18] = "Xbalanque";
  } else if (c == "Warrior") {
    // Warrior
    // There are 16 warriors
    var god = new Array();
    god[0] = "Achilles";
    god[1] = "Amaterasu";
    god[2] = "Bellona";
    god[3] = "Vamana";
    god[4] = "Tyr";
    god[5] = "Sun Wukong";
    god[6] = "Nike";
    god[7] = "Odin";
    god[8] = "Osiris";
    god[9] = "Hercules";
    god[10] = "Horus";
    god[11] = "Cu Chulainn";
    god[12] = "Chaac";
    god[13] = "Erlang Shen";
    god[14] = "Guan Yu";
    god[15] = "King Arthur";

  } else if (c == "Assassin") {
    // Assassin
    // There are 21 assassins
    var god = new Array();
    god[0] = "Arachne"; //     
    god[1] = "Awilix";
    god[2] = "Bakasura";
    god[3] = "Bastet";
    god[4] = "Camazotz";
    god[5] = "Da Ji";
    god[6] = "Fenrir";
    god[7] = "Hun Batz";
    god[8] = "Kali";
    god[9] = "Loki";
    god[10] = "Mercury";
    god[11] = "Ne Zha";
    god[12] = "Nemesis";
    god[13] = "Pele";
    god[14] = "Ratatoskr";
    god[15] = "Ravana";
    god[16] = "Serqet";
    god[17] = "Set";
    god[18] = "Susano";
    god[19] = "Thanatos";
    god[20] = "Thor";

  } else if (c == "Mage") {
    // Mage
    // 32 Mages in the list currently
    var god = new Array();
    god[0] = "Agni";
    god[1] = "Ah Puch";
    god[2] = "Anubis";
    god[3] = "Ao Kuang";
    god[4] = "Aphrodite";
    god[5] = "Baron Samedi";
    god[6] = "Chang'e";
    god[7] = "Chronos";
    god[8] = "Discordia";
    god[9] = "Freya";
    god[10] = "Hades";
    god[11] = "He Bo";
    god[12] = "Hel";
    god[13] = "Hera";
    god[14] = "Isis";
    god[15] = "Janus";
    god[16] = "Kukulkan";
    god[17] = "Merlin";
    god[18] = "Nox";
    god[19] = "Nu Wa";
    god[20] = "Olorun";
    god[21] = "Persphone";
    god[22] = "Poseidon";
    god[23] = "Ra";
    god[24] = "Raijin";
    god[25] = "Scylla";
    god[26] = "Sol";
    god[27] = "The Morrigan";
    god[28] = "Thoth";
    god[29] = "Vulcan";
    god[30] = "Zeus";
    god[31] = "Zhong Kui";

  } else {
    // Guardian
    // There are 19 guardians
    var god = new Array();
    god[0] = "Ares"; //     
    god[1] = "Artio";
    god[2] = "Athena";
    god[3] = "Bacchus";
    god[4] = "Cabrakan";
    god[5] = "Cerberus";
    god[6] = "Fafnir";
    god[7] = "Ganesha";
    god[8] = "Geb";
    god[9] = "Jormungandr";
    god[10] = "Khepri";
    god[11] = "Kumbhakarna";
    god[12] = "Kuzenbo";
    god[13] = "Sobek";
    god[14] = "Sylvanus";
    god[15] = "Terra";
    god[16] = "Xing Tian";
    god[17] = "Yemoja";
    god[18] = "Ymir";
  }

  // Math shit
  var ran = Math.random();
  var zed = Math.floor(god.length * ran);

  // Output
  document.getElementById("god").innerHTML = god[zed];

  return god[zed];

} // End of god function

// ---------------------------------------------------------------------------------------------------------
// BOOTS --------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------

function rollBoots() {
  // Choosing boots

  // Calls the result of the function checkClass
  var array = checkClass();
  var type = array[0];

  // dLog6, show what array[0] is
  document.getElementById("dLog6").innerHTML = "Developer log 6: " + type;

  var god = rollGod();

  // Determine what kind of god it is
  // Physical God
  if (type == "Physical") {
    // Test if it's Rat, if not continue
    if (god == "Ratatoskr") {
      var boot = new Array()
      boot[0] = "Acorn";
    } else {
      var boot = new Array();
      boot[0] = "Warrior Tabi";
      boot[1] = "Ninja Tabi";
      boot[2] = "Reinforced Greaves";
      boot[3] = "Talaria Boots";
    }
  } // End of if statement
  // Magical God
  else {
    var boot = new Array();
    boot[0] = "Shoes of Magi";
    boot[1] = "Shoes of Focus";
    boot[2] = "Reinforced Shoes";
    boot[3] = "Traveler Shoes";
  } // End of else statement

  // Output
  // Math and computer shit
  var ran = Math.random();
  var i = Math.floor(boot.length * ran);

  // Write it out
  document.getElementById("boots").innerHTML = boot[i];
}

// -----------------------------------------------------------------------------------------------------------------------
// First time opening the script
// -----------------------------------------------------------------------------------------------------------------------
// Function to create a class array and select a class
rollClass();
checkClass();
rollBoots();
<p1>God: </p1>
<div align="center" ;>
  <p2 id="god">God Here</p2>
</div>
<br>
<p1>Boots: </p1>
<div align="center" ;>
  <p2 id="boots">Boots Here</p2>
</div>

<p> Developer log: </p>
<p id="dLog1"> This shows the i value for variable: classList </p>
<p id="dLog3"> dLog3 </p>
<p id="dLog2"> dLog2 </p>
<p id="dLog4"> dLog4 </p>
<p id="dLog5"> dLog5 </p>
<p id="dLog6"> dLog6 </p>
<p id="dLog7"> dLog7 </p>

我有一个函数checkClass(),用于确定变量typetypeC。函数checkClass()返回这两个变量。变量type只能是"Physical""Magical"

然后,我有了第二个函数rollBoots(),该函数只需要变量type。由于某些原因,返回值在函数rollBoots()中进行了切换。例如,checkClass()具有变量type = "Physical"。但是,在函数rollBoots()中调用变量type = "Magical"时。

我将函数checkClass()转换为数组,以便可以在其他函数中调用它。在另一个函数中,变量typeC可以正常工作。我还看到函数checkClass()正确地输出了预期的变量。但是在变量type混用的过程中。

当前输出是选择了Magical,但它从Physical数组列表中创建了一个数组(Warrior Tabi,Ninja Tabi等)。

在代码段中,Developer Log 6和Variable a应该显示相同。有时候它有用,有时候却没有。

0 个答案:

没有答案