2015-03-25 1 views
0

Я пытаюсь закончить создание маленькой игры, которую я делаю для практики Javascript, но когда я открываю файл в браузере, он показывает все теги HTML, но что делает его особенно странным, так это то, что теги даже в том порядке, в котором я их. Ниже приведен код, который отображается на экране при его открытии.Как исправить этот код html/Javascript, появляющийся на экране после открытия в браузере?

<!DOCTYPE html> 
<html> 
<body> 

<script> 





</script> 

</body> 
</html> 

Код, приведенный ниже, является моим полным файлом. Вам не нужно читать Javascript, если вы не думаете, что это что-то связано с этим.

<!DOCTYPE html> 
<html> 
    <head> 
     <title>My Game</title> 
     <script type="text/javascript"> 
var user = prompt("Are you a goblin, knight, troll, human, or wizard?").toLowerCase() 
var name = prompt("What is your name?").toLowerCase() 
var gender = prompt("Are you male or female?").toLowerCase() 
switch (user) { 
    case "goblin": 
     console.log("Hello, you are going to have a fun journey as a goblin"); 
     var meetAnt = prompt(
      "You run into a giant ant eater, you can attack or run. Do you choose to attack?" 
     ).toLowerCase() 
     if (meetAnt == "yes") { 
      console.log(
       "you stab him in the eye, but then he hits you with his trunk. You are injured" 
      ); 
      var finishAnt = prompt(
       "you can either risk your life and finish him, or try to run away. Do you finish him?" 
      ).toLowerCase() 
      if (finishAnt == "yes") { 
       console.log("congratulations you have killed the aint eater!") 
      } else if (finishAnt == "no") { 
       console.log(
        "You run away and barley make it, you are badly hurt"); 
      } else { 
       console.log("sorry thats not an option") 
      } 
     } else if (meetAnt == "no") { 
      console.log(
       "You run past the ant eater and get to saftey. You are very lucky" 
      ) 
     } else { 
      console.log("Sorry that is not an option.") 
      break; 
      case "knight": 
       console.log(
        "Hello, you are going to have a fun journey as a knight" 
       ); 
       var dayFight = prompt(
        "You are versing the goblins in a fight, you need to choose how many days you go to fight(1-100). Remember if you choose too little days then you won't have stayed long enough to win the fight but if you stay to long you have more of a chance of dying or getting injured" 
       ).toLowerCase() 
       if (dayFight = parseInt(dayFight, 10); { 
         console.log(
          "You did not fight enough days, which has resulted in your kingdome loosing the war to the goblins." 
         ); 
        } else if (dayFight > 60) { 
         console.log(
          "You have went to war to long and been seriously injured." 
         ); 
        } else if (40 <= dayFight && dayFight <= 60) { 
         console.log(
          "You have been at war for the right amount of time. You have came out of the war with no injuries and you also have defieted the goblins!" 
         ) 
        } 
        break; 
        case "troll": 
         console.log(
          "Hello, you are going to have a fun journey as a troll" 
         ); 
         var red = true; 
         var green = true; 
         var yellow = false; 
         var blue = false; 
         var houseRaid = prompt(
          "You see four huts in the middle of the woods. You can raid two. There is a red hut, blue hut, yellow hut, and green hut. What is one color house that you want to raid?" 
         ).toLowerCase() 
         var doorPick = prompt(
          "Do you want to enter throuhg the back door or front door?" 
         ).toLowerCase() 
         if (doorPick || houseRaid == "red" || "green" && 
          "back door") { 
          console.log(
           "You raided these houses and left before any villagers would see you" 
          ); 
         } else { 
          console.log(
           "You raided those houses, but one of them was a booby trap and you are now captured" 
          ); 
         } 
         break; 
        case "human": 
         console.log(
          "Hello, you are going to have a fun journey as a human" 
         ); 
         var reinforceFound = prompt(
          "You know a storm is comming and you have to reinforce your hut, but you only have enough material to reinforce either your lower foundations or higher foundations. Which do you inforce? Higher or lower?" 
         ).toLowerCase() 
         if (reinforceFound == "lower") { 
          console.log(
           "The storms winds pushed down the top of your house and caved in your roof." 
          ); 
         } else if (reinforceFound == "higher") { 
          console.log(
           "The storm did not do that much damage to your house due to your reinforced higher foundations. Good choice" 
          ); 
         } else { 
          console.log(
           "sorry but that is not an option. Pick either 'higher', or 'lower'" 
          ) 
         } 
         break; 
        case "wizard": 
         console.log(
          "Hello, you are going to have a fun journey as a wizard" 
         ); 
         var blood = true; 
         var dust = true; 
         var wings = false; 
         var mushrooms = false; 
         var postion = prompt(
          "You are working on a new healing potion but you do not know what you need to add to finish it. You have 4 ingrediants; blood, dust, wings, mushrooms. Pick one too add. (WARNING: Pick carefully because if you choose the wrong ingerdiant, then your potion will be ruined.)" 
         ).toLowerCase() 
         if (postion == "wings" || postion == "mushroom") { 
          console.log(
           "You picked a bad ingrediant and now your potion is ruined." 
          ); 
         } else if (postion = "dust" || "blood") { 
          console.log(
           "you picked the right ingrediant and your potion is okay" 
          ) 
         } else { 
          console.log("sorry but that is not an option"); 
         } 
         break; 
        default: 
         console.log(
          "Sorry but that is not a character in the game"); 
       }; 

</script> 
    </head> 
    <body> 
     <h1 style="font-family:Arial; align:center">My Game</h1> 
</body> 
</html> 


    [1]: http://i.stack.imgur.com/nYseA.png 
+2

В каком расширении файла у вас есть этот файл, сохраненный как? – NewToJS

+0

Вы открыли этот файл? Или вы по ошибке открыли другой файл? – tsh

+0

Эта страница открывается, как обычно, она содержит несколько синтаксических ошибок javascript, но она не открывается в браузере как веб-страница. ** '.html' ** – NewToJS

ответ

0

Я использовал код, который вы опубликовали. Я не видел никаких HTML и Javascript-кода в браузере, но есть 2 синтаксических ошибки.

Здесь находятся места, где присутствует ошибка.

1] Вам не хватает «}» в другой части корпуса «гоблин» чуть выше перерыва; для этого случая.

2] В случае, если «рыцарь» на линии первого «если» вам не хватает »)» и «;» после "parseInt (dayFight, 10)" не требуется.

Есть много других мест, где вам не хватает ";" после операторов console.log.

Вам необходимо внимательно проверить синтаксис кода, который вы пишете.

Другое дело, использовать какой-то инструмент для отладки для Javascript. У всех браузеров встроенный инструмент отладки. Вы бы впервые обнаружили эти ошибки при открытии этой страницы в браузере.

Смежные вопросы