在函数内部时,Javascript代码未运行

时间:2016-07-20 05:50:30

标签: javascript

我正在用html和javascript制作一个游戏并且使用了'控制台'我使用了IF语句来允许用户在游戏中导航。但是,他们似乎停止了工作。它们允许用户在游戏中移动。为什么IF语句不能运行?如何解决这个问题呢?

function runcmd(){
	var user = document.getElementById('code').value;
	if(user == 'clear'){
		l1.innerHTML = '';
		l2.innerHTML = '';
		l3.innerHTML = '';
		l4.innerHTML = '';
		l5.innerHTML = '';
		l6.innerHTML = '';
		l7.innerHTML = '';
		l8.innerHTML = '';
		l9.innerHTML = '';
		l10.innerHTML = '';
	};
	if(user == 'connect'){
		var user = document.getElementById('code').value;
		l1.innerHTML = 'Connecting to ' ;
		l2.innerHTML = 'Connected to ' ;
		connected = true;
		l4.innerHTML = 'view bank';
		l5.innerHTML = 'upload [virus]';
		l8.innerHTML = 'disconnect [ip]';
		if(user == 'disconnect'){
			connected = false;
			l1.innerHTml = 'Disconnected Safely...';
		};
		if(user == 'view bank'){
			var pwrd = Math.floor( Math.random() * 1) - 10000;
			var nam = Math.floor( Math.random() * 1) - 10000;
			alert(pwrd);
		    alert(nam);
			var uname = 'user' + nam;
			var user = prompt('Username: ');
			var pass = prompt('Password: ');
			if(user == uname && pass == pwrd ){
			}
		};	
		if(user == 'upload'){
		    svirus= prompt('Enter Virus: ');
			for(var key in boughtviruses) {
				if(boughtviruses[key] == svirus) {
					l1.innerHTML = 'Uploading ' + svirus;
					l2.innerHTML = 'Virus Uploaded';
				}else{
					alert("You Don't Have This Virus!");
		  	};
            };
		};
	};
};
			.console{
				position: relative;
				height: 250px;
				width: 500px;
				background-color: #000;
				border-left: 10px solid #cccccc;
				border-top: 5px solid #cccccc;
				border-right: 10px solid #cccccc;
				border-bottom: 10px solid #cccccc;
			}
			.exit {
				float: right;
				background: #800000;
				color: white;
				height: 25px;
				border: none;
				width: 40px;
				font-size: 20px;
				text-align: center;
				margin-left: 5px;
			}
			<div id="con" class="console">
				<button class="exit" onclick="hidecon()">X</button>
					<span class="span" id="l1" style="width:50px"></span><br />
					<span class="span" id="l2" style="width:50px"></span><br />
					<span class="span" id="l3" style="width:50px"></span><br />
					<span class="span" id="l4" style="width:50px"></span><br />
					<span class="span" id="l5" style="width:50px"></span><br />
					<span class="span" id="l6"  style="width:50px"></span><br />
					<span class="span" id="l7" style="width:50px"></span><br />
					<span class="span" id="l8" style="width:50px"></span><br />
					<span class="span" id="l9"  style="width:50px"></span><br />
					<span class="span" id="l10"  style="width:50px"></span><br />
				<span style="position: absolute; left: 0; bottom: 0;color:#66ff33;">C:\></span><input onclick="this.select()" id="code" class="inp"/>
				<button style="border:none;position:absolute;background-color:black;bottom:0;right:114;color:#66ff33;border-radius: 5px;border: 1px solid white;" onclick="runcmd()">Send Command</button>
			</div>

2 个答案:

答案 0 :(得分:1)

当你输入&#39; -10000&#39;用于密码和&#39; user-10000&#39;用户名

答案 1 :(得分:0)

据我了解,您正在尝试创建用户名&amp;密码通过生成随机整数。

您的解决方案存在问题,因为Math.random()的值始终为0和0之间。 1,对其应用Math.floor()将始终提供值0

因此,您的用户名和密码将始终具有-10000值,这是我认为您不想要的。

相反,请尝试执行Math.floor(Math.random()*10000),它会为您提供100010000范围内的随机整数。