r/CritiqueMyCode • u/Eddsbutt • Feb 22 '15
[Java] A coding virgin's attempt at entertainment, that for some reason or other doesnt work, help?
<!doctype html>
<html> <body>
<h1>Autism Test</h1>
<p id="input">Please input a number between :</p>
<p id="#1"></p>
<p>and</p>
<p id="#2"></p>
<input id="numb" type="number">
<button type="button" onclick="myFunction()">Submit</button>
<p id="demo"></p>
<script> function randomNumber1() { var z = Math.floor((Math.random() * 100) + 1); document.getElementById("#1").innerHTML = z; } function randomNumber2() { var y = Math.floor((Math.random() * 100) + 1); document.getElementById("#2").innerHTML = y; } function myFunction() { var x, text; x = document.getElementById("numb").value; if (x < n || x > m) { <p>Try again!</p> text = " "; } else { <p>Well done, you aren't autistic</p> text = " "; } document.getElementById("demo").innerHTML = text; } function logic() { if (y < z) { y = n; z = m; } else { z = m; y = n; } } randomNumber1(); randomNumber2(); logic(); </script>
</body> </html>
4
u/EhlMalus Feb 22 '15
If you're just starting out, i strongly recommend that you get into the habit of formatting your code in a way that's easy to read.
function randomNumber1(){
var z = Math.floor((Math.random() * 100) + 1);
document.getElementById("#1").innerHTML = z;
}
function randomNumber2(){
var y = Math.floor((Math.random() * 100) + 1);
document.getElementById("#2").innerHTML = y;
}
function myFunction(){
var x, text;
x = document.getElementById("numb").value;
if (x < n || x > m) {
<p>Try again!</p>
text = " ";
} else {
<p>Well done, you aren't autistic</p>
text = " ";
}
document.getElementById("demo").innerHTML = text;
}
function logic(){
if (y < z) {
y = n;
z = m;
} else {
z = m;
y = n;
}
}
randomNumber1();
randomNumber2();
logic();Formatting like this helps a lot when debugging. For instance, i can instantly see one problem you're having is variable scope.
The variable z that you declare in randomNumber1() can only be read in the function you declared it in. You'll want to declare z and y outside of any function to give it a global scope. That way you can actually use it in logic(). Either that, or read up on functions and how to send/accept parameters.
Speaking of logic(), i'm not seeing where the variables n and m are declared. It seems to me that they don't really exist anywhere.
Lastly, your if-statements inside of myFunction(), i think you meant to put the <p> tags inside of text.
This is just what i saw as immediate issues, i hope this helps.
2
3
9
u/mcstafford Feb 22 '15
You might want to hold off on the condescension until you've at least written something that works... and/or differentiate between Java and JSP.