...

View Full Version : Higher or Lower Button



jokerjumper18
08-29-2010, 08:46 AM
I'm trying to make a higher or lower game but I can't seem to get the higher and lower buttons to work. I have my deck set up in an array such as




var deck = [
{picture: "images/AceHearts.png", facevalue: 1 },
{picture: "images/2Hearts.png", facevalue: 2 },
etc.
]

<!-- Then the rest of my code is as follows>

function rnd()
{
return Math.random()-0.5;
}


function hidemessage(){
document.getElementById("winningmessage").style.display = "none";
}




hand1 = new Array();

deck.sort(rnd);

<!-- This function randomly picks a card and takes its value and returns it --!>
function getHandHTML(hand)
{

html= "";
total = 0;
for (i=0; i<hand.length; i++)
{
html+= "<img src = \""+ hand[i].picture +"\"> ";
total= hand[i].facevalue;
}

return html;
}



<!-- This function takes a card from the deck --!>
function nextcard()
{

for (i=0; i< 1; i++)
{
card = deck.shift();
hand1.push(card);
}

document.getElementById('hand1').innerHTML = getHandHTML(hand1);
document.getElementById("winningmessage").style.display = "none";
}



<!-- This function adds takes the next card from the deck and places it on the screen --!>
<!--It also disables the buttons after 6 cards have been drawn --!>

function addCardToHand()
{
card = deck.shift();
if (card == null) { return;}
hand1.push(card);
document.getElementById('hand1').innerHTML = getHandHTML(hand1);

if(i==6){
document.getElementById("higher").disabled = true;
document.getElementById("lower").disabled = true;
document.getElementById("winningmessage").style.display = "block";
}
}

window.onload = nextcard;

function higher(){

if (hand1[index].facevalue() > hand1[index-1].facevalue())
{
alert("higher");
}
else
{
alert("wrong")
}
}

</script>

<body>

<style>

.hand {margin: 20px; padding: 20px;}
</style>

<input type = button value = "higher" onclick = "addCardToHand();higher()" id = higher>
<input type = button value = "lower" onclick = "addCardToHand()" id = lower>
<div class = hand id = hand1></div>


<div id = "winningmessage">
You Win
</div>


</body>
</html>



I've highlighted the area where I've attempted to make an alert box pop up saying if the higher button was right or wrong.
Any suggestions would be much appreciated

Old Pedant
08-29-2010, 09:20 PM
Maybe if you gave us the rules of the game we could understand it.

Some of your code makes no sense.

For example:


for (i=0; i< 1; i++)
{
card = deck.shift();
hand1.push(card);
}

is doing exactly the same thing that simply

card = deck.shift();
hand1.push(card);
would do.

Or, for that matter, what

hand1.push( deck.shift() );

would do.

jokerjumper18
08-30-2010, 01:42 PM
Thanks for your reply.
It's just a simple higher or lower game that I'm trying to make. I know that I'm a bit all over the place with my code but I've only recently started learning it.
I was thinking of having two different hands so that is why there was a for loop.
If you would have any suggestions on how to compare the values of the current and last cards it would be much appreciated.
I think it would be something along the lines of


if (hand1[index].value() > hand1[index-1].value())



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum