为什么我的JavaScript代码不能使用prompt和innerHTML?

时间:2017-02-15 14:03:50

标签: javascript user-input

我正在尝试制作在线角色扮演游戏,但我的代码无效。它询问用户他们想要命名他们的角色,以及他们想要的种族。然后它将为他们随机选择一些统计数据,并根据他们的种族增加和减少他们的统计数据。

    var nameAndRaceFunction = function(){       
        var namePrompt = prompt("What shall you name your character?");
        var racePrompt = prompt("What race shall your character be? Please spell it correctly, with no capitals.");             
        var race = racePrompt.toLowerCase();
        var totalSentence = namePrompt + " the " + race;
        document.getElementById("nameAndRace").innerHTML = totalSentence;
    }

    var str = Math.floor((Math.random() * 10) + 1);
    var int = Math.floor((Math.random() * 10) + 1);
    var hlth = Math.floor((Math.random() * 10) + 1);
    var dext = Math.floor((Math.random() * 10) + 1);

    var getStatFunction = function(){
        if(racePrompt === "elf"){
            elfStats();
        }else if(racePrompt === "dwarf"){
            dwarfStats();
        }else if(racePrompt === "man"){
            manStats();
        }else{

        }
    }

       var elfStats = function(){
           var elfStr = str;
           var elfInt = int + 1;
           var elfHlth = (hlth - 1)*10;
           var elfDext = dext + 1;
           document.getElementById("strength").innerHTML = elfStr;
           document.getElementById("intelligence").innerHTML = elfInt;
           document.getElementById("health").innerHTML = elfHlth;
           document.getElementById("dexterity").innerHTML = elfDext;
       }

       var manStats = function(){
           var manStr = str + 2;
           var manInt = int;
           var manHlth = (hlth - 1) * 10;
           var manDext = dext;
           document.getElementById("strength").innerHTML = manStr;
           document.getElementById("intelligence").innerHTML = manInt;
           document.getElementById("health").innerHTML = manHlth;
           document.getElementById("dexterity").innerHTML = manDext;
       }

       var dwarfStats = function(){
           var dwarfStr = str + 1;
           var dwarfInt = int;
           var dwarfHlth = (hlth + 1) * 10;
           var dwarfDext = dext - 1;
           document.getElementById("strength").innerHTML = dwarfStr;
           document.getElementById("intelligence").innerHTML = dwarfInt;
           document.getElementById("health").innerHTML = dwarfHlth;
           document.getElementById("dexterity").innerHTML = dwarfDext;
       }

2 个答案:

答案 0 :(得分:0)

应该使用race作为参数调用 getStatFunction,或者应该将变量racePrompt outsite定义为函数

另外,如果你想制作一个播放器,一个好主意应该是将它作为一个对象并使用nameAndRaceFunction,就像一个构造函数

答案 1 :(得分:0)

racePromptnameAndRaceFunction()函数范围内定义。它不在外面。更进一步:你小写它,所以稍后我只会检查race而不是racePrompt

一种解决方案是将race全局设为strinthlthdext

  var nameAndRaceFunction = function() {
    namePrompt = prompt("What shall you name your character?");
    var racePrompt = prompt("What race shall your character be? Please spell it correctly, with no capitals.");
    race = racePrompt.toLowerCase();
    var totalSentence = namePrompt + " the " + race;
    document.getElementById("nameAndRace").innerHTML = totalSentence;
    getStatFunction()
  }

  var namePrompt, race; 
  var str = Math.floor((Math.random() * 10) + 1);
  var int = Math.floor((Math.random() * 10) + 1);
  var hlth = Math.floor((Math.random() * 10) + 1);
  var dext = Math.floor((Math.random() * 10) + 1);

  var getStatFunction = function() {
    if (race === "elf") {
      elfStats();
    } else if (race === "dwarf") {
      dwarfStats();
    } else if (race === "man") {
      manStats();
    } else {

    }
  }