简体   繁体   中英

Javascript code not running when inside a function

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> 

当您输入密码“ -10000”和用户名“ user-10000”时,可以正常工作

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM