0

I am making a game in html and javascript and have used a 'console' I used IF statements to allow the user to navigate around the game. But, they have seemed to stop working. They allow the user to move around in the game. Why aren't the IF statements running and how can I fix this?

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>

8
  • 2
    doesn't run as in, what are you expecting ? Commented Jul 20, 2016 at 5:53
  • what is user,uname,pass and pwrd ? Commented Jul 20, 2016 at 5:53
  • "doesn't seem to run" What is expected result? How would user be aware of exact random number set at nam or pwrd? Commented Jul 20, 2016 at 5:53
  • @were matrix generating user name at random will generate duplicate someday Commented Jul 20, 2016 at 5:55
  • works totally fine , hope you are providing user user-10000 and password -10000 Commented Jul 20, 2016 at 5:55

2 Answers 2

1

Working perfectly when u enter '-10000' for password and 'user-10000' for username

Sign up to request clarification or add additional context in comments.

Comments

0

As far as I understand, you are trying to create username & password via generating random integers.

The problem with your solution is, as Math.random() will always have values between 0 & 1, applying Math.floor() to it will always provide the value 0.

So, your usernames and password will always have -10000 value, which is something I believe you do not want.

Instead, try doing Math.floor(Math.random()*10000) which will give you random integer in the range of 1000 to 10000.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.