Jump to content

An accurate result in if statement


inktherapy

Recommended Posts

Hi,I am doing a random slot machine, with 3 number combinations, if Stan's numbers are "6 5 6" v.s. my numbers "6 5 6" in the same order, will display "you got the same combinations!" else "try again" <--- these are fine with me.My problem now is, I want a special combination like "6 6 6" but it didn't work right, for example: Stan's numbers "6 6 6" against my number "6 5 6" it will still display "666 the number of the beast, you won a ticket to Iron Maiden concert!" but I don't want that, the result should be like: Stan's numbers "6 6 6' and mine should also be "6 6 6" to be able to display "...you won a ticket to Iron Maiden concert!".Please teach me how to solve this, I'd like to know more... thanks

<html><head><script type="text/javascript">function pressMe (){a = new Array();b = new Array();for (i=0; i<=2; i++){a[i]= Math.round(Math.random()+5);b[i] = Math.round(Math.random()+5);}if (b[0] == a[0] && b[1] == a[1] && b[2] == a[2]) {alert("You got the same combination");} else if (b[0] && a[0] && b[1] && a[1] && b[2] && a[2] ==6) {alert("666 the number of the beast, you won a ticket to Iron Maiden concert!");} else{alert("You lost your soul to Stan!");}document.write("<strong>Stan's Random ###### Machine!</strong>" + "<br /> ");document.write("<i>Stan's Numbers</i>" + "<br />");for (i in b){document.write(b[i] + " ");}document.write("<br />" + "<i>Your numbers!</i>" + "<br />");for (i in a) {document.write(a[i] + " ");}}</script></head><body><input type="button" value="I will bet my soul!" onclick="pressMe()"></input></body></html>

Link to comment
Share on other sites

Here's you problem:

} else if (b[0] && a[0] && b[1] && a[1] && b[2] && a[2] ==6) {

I know it seems like it should do what you want, but it doesn't. Your statement can be translated like this:if b[0] is not false AND a[0] is not false . . . AND a[2] equals 6 . . .Here's what you need:

} else if (b[0] == a[0] == b[1] == a[1] == b[2] == a[2] == 6) {

which is shorthand for this:

} else if (b[0] == 6 && a[0] == 6 && b[1] == 6 && a[1] == 6 && b[2] == 6 && a[2] == 6) {

Link to comment
Share on other sites

Here's what you need:
} else if (b[0] == a[0] == b[1] == a[1] == b[2] == a[2] == 6) {

Umm... I believe that will evaluate like this:
} else if ((((((b[0] == a[0]) == b[1]) == a[1]) == b[2]) == a[2]) == 6) {

And that's true as long as the first two values are equal and the rest are truthy (not false, null, undefined, 0, or the empty string).

Link to comment
Share on other sites

Umm... I believe that will evaluate like this:
} else if ((((((b[0] == a[0]) == b[1]) == a[1]) == b[2]) == a[2]) == 6) {

And that's true as long as the first two values are equal and the rest are truthy (not false, null, undefined, 0, or the empty string).

It starts evaluating from the last expression and moving backwards.The same way you can assign a number to several values:$A = $B = $C = 5;you can compare them like that too.Or that's as far as I can remember.
Link to comment
Share on other sites

Hi,Thanks Deirdre's Dad and thank you guys for the brief explanation, I will later test it :) I was thinking that if storage1 and storage2... equals to 6 will all check to true, but I was wrong... I will also study further in && thing. Again thank you guys.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...