PDA

View Full Version : Check if num==num2



TimFA
11-21-2007, 09:59 PM
var ranNum= Math.floor(Math.random()*5);
function getNum()
{
var guessNum= getElementsByName("number");
}
function checkNum()
{
if (ranNum==guessNum)
{
alert("You win!")
}
else
{
alert("You lose!")
}
}


Will this check if inside a form the box named "number" equals the random number? I can't seem to get it working.

Trinithis
11-21-2007, 10:45 PM
ranNum==guessNum.value

codeexploiter
11-22-2007, 03:34 AM
var ranNum= Math.floor(Math.random()*5);
var guessNum;
function getNum(){
guessNum= getElementsByName("number");
}

function checkNum(){
if(ranNum == guessNum[0].value){
alert("You win!")
}else{
alert("You lose!")
}
}

I've made in guessNum variable global as in your code its scope limited to getNum function only.

The getElementsByName() method returns a collection of objects with the specified NAME (Array of objects). Even if it returns only one object it return it as array so you need to access the object using an index.

Twey
11-22-2007, 03:45 AM
With some proper formatting your errors become clearer:
var ranNum= Math.floor(Math.random()*5);

function getNum() {
// getElementsByName() is a method of document,
// and needs to be accessed as such.
// Also, it returns a collection of elements.

// Using an ID and getElementById() would probably
// be more appropriate.
var guessNum= getElementsByName("number");
}

function checkNum() {
// guessNum was defined locally to getNum(),
// and so isn't available here.
if (ranNum==guessNum) {
alert("You win!")
} else {
alert("You lose!")
}
}How I'd do it:
var NumberGuess = {
randNum: (function() {
var a = function() {
var n = Math.floor(Math.random() * 5);
a.toString = function() { return n; };
return a;
};
return a();
})(),
checkNum: function() {
alert(document.getElementById("number").value
== NumberGuess.randNum
? "You win"
: "You lose");
}
}

TimFA
11-22-2007, 02:59 PM
How will it check?? I can't get it to submit the form to it, just displays the JavaScript (as expected), and if you just submit it, nothing happens. I read to code, and don't see any functions that need to be activated.

Twey
11-22-2007, 03:06 PM
<form action="" onsubmit="NumberGuess.checkNum(); return false;">
<div>
<label>Guess:
<input type="text" id="number">
</label>
<input type="submit" value="Check">
</div>
</form>

TimFA
11-22-2007, 03:16 PM
Still doesn't work.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title></title>
<script type="JavaScript">
var NumberGuess = {
randNum: (function() {
var a = function() {
var n = Math.floor(Math.random() * 5);
a.toString = function() { return n; };
return a;
};
return a();
})(),
checkNum: function() {
alert(document.getElementById("number").value
== NumberGuess.randNum
? "You win"
: "You lose");
}
}
</script>
</head>
<body>
<form action="" onsubmit="NumberGuess.checkNum(); return false;">
<div>
<label>Guess:
<input type="text" id="number">
</label>
<input type="submit" value="Check">
</div>
</form>
</body>
</html>

jscheuer1
11-22-2007, 04:00 PM
Usually something like this won't hurt:


<script type="JavaScript">

But, in this case it does. It should be:


<script type="text/javascript">

The type attribute is required, but many browsers will let you get away without it and/or accept the decremented language attribute. However, if you do use either, it should be used correctly. Best practice is to use the type attribute only, and to write it exactly as I have shown.

Twey
11-22-2007, 05:03 PM
The browser doesn't know what to do with a script of type "JavaScript" so ignores it.
the decremented language attributeHeh, I think you mean "deprecated" :)

jscheuer1
11-22-2007, 05:12 PM
Heh, I think you mean "deprecated" :)

Decapitated?

Twey
11-22-2007, 05:36 PM
Is that what they do to pedants where you live? :p

jscheuer1
11-22-2007, 05:42 PM
Is that what they do to pedants where you live? :p

If so, perhaps you'd better watch out if you ever cross the pond. But no, that's not what I meant at all. I did use a question mark after all.

TimFA
11-24-2007, 07:59 PM
Thank you very much. Now two more things, if not hard, I was using the if and else events the way I did so that I could fire off other events with it, how would that be down with this one? And would it be possible to insert a variable, like "You lose, correct number: +n+" or something, I tried many different ways, but I couldn't get it to work. I am not very good with JavaScript...

Twey
11-24-2007, 08:08 PM
I was using the if and else events the way I did so that I could fire off other events with it, how would that be down with this one?Put whatever number you like in checkNum(). NumberGuess.randNum is the number the machine is currently thinking of, NumberGuess.randNum() is the number the machine is currently thinking of after generating a new one.
var NumberGuess = {
randNum: (function() {
var a = function() {
var n = Math.floor(Math.random() * 5);
a.toString = function() { return n; };
return a;
};
return a();
})(),
checkNum: function() {
alert(document.getElementById("number").value
== NumberGuess.randNum
? "You win"
: "You lose. Correct number was: " + NumberGuess.randNum);
}
}

jscheuer1
11-24-2007, 08:25 PM
You can also add : ? pairs:


<script type="text/javascript">
var NumberGuess = {
randNum: (function() {
var a = function() {
var n = Math.floor(Math.random() * 5);
a.toString = function() { return n; };
//alert(a);
return a;
};
return a();
})(),
checkNum: function() {
alert(document.getElementById("number").value
== NumberGuess.randNum
? "You win, the number is "+NumberGuess.randNum
:document.getElementById("number").value
< NumberGuess.randNum
? "You loose, number is higher, number is "+NumberGuess.randNum
: "You lose, number was lower, number is "+NumberGuess.randNum);
}
}
</script>

Twey
11-25-2007, 09:05 AM
I don't know about you, but I find nested ternary operators pretty difficult to read.

jscheuer1
11-25-2007, 02:07 PM
I don't know about you, but I find nested ternary operators pretty difficult to read.

I agree, it simply is something the OP asked about. I tried to render it in the same format as what you already had done, but I think I got one or more of the indents off a little. It works, and again, yes it's hard to follow.

It would probably be better to scrap that whole section in favor of if/else if/else type syntax:


checkNum: function() {
var n = NumberGuess.randNum, q = document.getElementById("number").value,
a = "You lose, number was lower, number is " + n;
if (n == q)
a = "You win, the number is " + n;
else if (q < n)
a = "You loose, number is higher, number is " + n;
alert(a);
}

Twey
11-25-2007, 02:23 PM
Ah, it would be:
alert(document.getElementById("number").value
== NumberGuess.randNum
? "You win, the number is "+NumberGuess.randNum
: document.getElementById("number").value
< NumberGuess.randNum
? "You loose, number is higher, number is "
+ NumberGuess.randNum
: "You lose, number was lower, number is "
+ NumberGuess.randNum);The format is:
condition
? true-action
: false-actionThe condition above was split over two lines because it was too long, rather than being an actual new statement or new block; for such cases, when they start a block, I indent them twice to distinguish them from the block beneath.

I agree that if you're going to make it more complex an if/else branch is probably better though.

TimFA
11-28-2007, 05:41 PM
Was there some type of "one up the previous poster" competition I didn't know about? :p Well thanks guys, big help.

-Tim

jscheuer1
11-28-2007, 06:02 PM
Was there some type of "one up the previous poster" competition I didn't know about? :p Well thanks guys, big help.

-Tim

Twey and I work as a team, don't mess with it. :p

Twey
11-28-2007, 06:33 PM
Heh, it works!