JavaScript:显示由用户选择(在提示中插入)+计算机选择产生的图像

时间:2015-10-25 16:51:23

标签: javascript image prompt console.log

我有这段代码:

	        /*
			*
			* Game Part 1: Getting the user's choice and the computer's randomly choice 
			*
			*/
			var userChoice = prompt("What's your nature element?");
			var computerChoice = Math.random();
			// get computer's randomly choice by "if" conditioning
			if(computerChoice <= 0.25)
			{
				computerChoice = "water";
			}
			else if(computerChoice <= 0.50)
			{
				computerChoice = "fire";
			}
			else if(computerChoice <= 0.75)
			{
				computerChoice = "air";
			}
			else
			{
				computerChoice = "earth";
			}

			console.log("Computer's choosen element: " + computerChoice);

			// This function will show the elements combination image
			function show_combination_image(src) {
			    var img = document.createElement("img");
			    img.src = src;

			    // This line will just add it to the <body> tag
			    document.body.appendChild(img);
			}

			/*
			*
			* Game Part 2: Combining user's choice with computer's choice and returning the result
			*
			*/

			var combine = function(element1, element2)
			{
				
				// if the choosen elements are the same
				if(element1 === element2)
			{
				return "It seems that your element and computer's element are the same. You need to make a combination, therefore please insert a different element.";
			}
				
				// else if user chooses the "water" element
				else if(element1 === "water")
				{
					if(element2 === "fire")
					{
						show_combination_image("img/water+fire.jpg");
					}
					else if(element2 === "air")
					{
						show_combination_image("img/water+air.jpg");
					}
					else (element2 === "earth")
					{
						show_combination_image("img/water+earth.jpg");
					}
				}
				
				// else if user chooses the "fire" element
				else if(element1 === "fire")
				{
					if(element2 === "water")
					{
						show_combination_image("img/water+fire.jpg");
					}
					else if(element2 === "air")
					{
						show_combination_image("img/fire+air.jpg");
					}
					else
					{
						show_combination_image("img/fire+earth.jpg");
					}
				}
				
				// else if user chooses the "air" element
				else if(element1 === "air")
				{
					if(element2 === "water")
					{
						show_combination_image("img/water+air.jpg");
					}
					else if(element2 ==="fire")
					{
						show_combination_image("img/fire+air.jpg");
					}
					else
					{
						show_combination_image("img/air+earth.jpg");
					}
				}
				
				// else if user chooses the "earth" element
				else if(element1 === "earth")
				{
					if(element2 === "water")
					{
						show_combination_image("img/water+earth.jpg");
					}
					else if(element2 === "fire")
					{
						show_combination_image("img/fire+earth.jpg");
					}
					else
					{
						show_combination_image("img/air+earth.jpg");
					}
				}
			} 

			/*
			*
			* Game Part 3: Getting the user's element and the computer's randomly element and show the combination image
			*
			*/

			combine(userChoice, computerChoice);
<html>
  <head>
  </head>
  <body>

  </body>
</html>

我得到提示警告框,在我插入一些元素后,例如:水,火,土或空气,我只是得到空白页。 我希望显示由我选择的元素和计算机所选元素的组合产生的图像。

请告诉我代码有什么问题?

0 个答案:

没有答案