Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Simple JavaScript game gone awry due to possible math problem

    The program I'm having trouble with is a Slot Machine game I made in JavaScript. Everything works aesthetically fine, but the actual math part (calculating the money gained/lost from wins and losses) is doing something pretty strange....

    For instance, I bet $0.25 in one instance so the game subtracted $0.25 from my original total of $22.75 It then added $1.25 (the winning bonus for sevens is x5) to my dollar amount. Despite this, my value is showing up as 22.750.50.751.25 I have no idea what I might be doing wrong. I'm sure you will find many more instances of these strange glitches of it adding multiple decimals or thousands of dollars to the total whenever you win.

    The program is hosted here. I would post the bit of code I think may be causing the problem, but I really have no idea what could cause this. The best I can do is post the function that does the calculations for the slot machine change and the money values.

    Code:
    function slotMachine(betAmnt, betMoney) {
    	// alert("Still working out the bugs!")
    	
    	var imgChange = Math.floor(Math.random()*3+1)
    
    	var slot1 = document.getElementById('slot_1')
    
    	var slot2 = document.getElementById('slot_2')
    
    	var slot3 = document.getElementById('slot_3')
    
    	if (betMoney.value <= 0) {
    		betMoney.value = 0
    		alert("You lost all your money!")
    		document.getElementById('restart').style.display='inline'
    	}
    
    	else {
    
    		switch(imgChange) {
    			case 1: document.getElementById('slot_1').src="seven.png";
    				break;
    
    			case 2: document.getElementById('slot_1').src="gold.jpg";
    				break;
    
    			case 3: document.getElementById('slot_1').src="lemon.jpg";
    				break;
    		}
    
    		imgChange = Math.floor(Math.random()*3+1)
    
    		switch(imgChange) {
    			case 1: document.getElementById('slot_2').src="seven.png";
    				break;
    
    			case 2: document.getElementById('slot_2').src="gold.jpg";
    				break;
    
    			case 3: document.getElementById('slot_2').src="lemon.jpg";
    				break;
    		}
    
    		imgChange = Math.floor(Math.random()*3+1)
    
    		switch(imgChange) {
    			case 1: document.getElementById('slot_3').src="seven.png";
    				break;
    
    			case 2: document.getElementById('slot_3').src="gold.jpg";
    				break;
    
    			case 3: document.getElementById('slot_3').src="lemon.jpg";
    				break;
    		}
    
    		if (slot1.src == slot2.src && slot1.src == slot3.src && slot2.src == slot3.src) {
    			alert("YOU'RE A WINNER!")
    
    			if (slot1.src == "lemon.jpg")
    				alert("Lemon wins!")
    				betMoney.value = betMoney.value + (betAmnt.value * 2)
    
    			if (slot1.src == "gold.jpg")
    				alert("Gold wins!")
    				betMoney.value = betMoney.value + (betAmnt.value * 3)
    
    			if (slot1.src == "seven.png")
    				alert("Seven wins!")
    				betMoney.value = betMoney.value + (betAmnt.value * 5)
    		}
    
    		else {
    			betMoney.value = betMoney.value - betAmnt.value
    		}
    
    		if (betMoney.value < 0)
    			betMoney.value = 0
    	}
    }
    I've run through my code meticulously several times, finding no errors that I could detect, meaning it's a weakness of JavaScript or something I just don't know of... please help!

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,466
    Thanks
    3
    Thanked 495 Times in 482 Posts
    easier if you post the relavent HTML

    but you could try to make sure your adding numbers not strings

    Code:
    function slotMachine(betAmnt, betMoney) {
    	// alert("Still working out the bugs!")
    
    	var imgChange = Math.floor(Math.random()*3+1)
    
    	var slot1 = document.getElementById('slot_1')
    
    	var slot2 = document.getElementById('slot_2')
    
    	var slot3 = document.getElementById('slot_3')
    
    	if (betMoney.value <= 0) {
    		betMoney.value = 0
    		alert("You lost all your money!")
    		document.getElementById('restart').style.display='inline'
    	}
    
    	else {
    
    		switch(imgChange) {
    			case 1: document.getElementById('slot_1').src="seven.png";
    				break;
    
    			case 2: document.getElementById('slot_1').src="gold.jpg";
    				break;
    
    			case 3: document.getElementById('slot_1').src="lemon.jpg";
    				break;
    		}
    
    		imgChange = Math.floor(Math.random()*3+1)
    
    		switch(imgChange) {
    			case 1: document.getElementById('slot_2').src="seven.png";
    				break;
    
    			case 2: document.getElementById('slot_2').src="gold.jpg";
    				break;
    
    			case 3: document.getElementById('slot_2').src="lemon.jpg";
    				break;
    		}
    
    		imgChange = Math.floor(Math.random()*3+1)
    
    		switch(imgChange) {
    			case 1: document.getElementById('slot_3').src="seven.png";
    				break;
    
    			case 2: document.getElementById('slot_3').src="gold.jpg";
    				break;
    
    			case 3: document.getElementById('slot_3').src="lemon.jpg";
    				break;
    		}
    
    		if (slot1.src == slot2.src && slot1.src == slot3.src && slot2.src == slot3.src) {
    			alert("YOU'RE A WINNER!")
    
    			if (slot1.src == "lemon.jpg")
    				alert("Lemon wins!")
    				betMoney.value = betMoney.value*1 + (betAmnt.value * 2)
    
    			if (slot1.src == "gold.jpg")
    				alert("Gold wins!")
    				betMoney.value = betMoney.value*1 + (betAmnt.value * 3)
    
    			if (slot1.src == "seven.png")
    				alert("Seven wins!")
    				betMoney.value = betMoney.value*1 + (betAmnt.value * 5)
    		}
    
    		else {
    			betMoney.value = betMoney.value*1 - betAmnt.value*1
    		}
    
    		if (betMoney.value < 0)
    			betMoney.value = 0
    	}
    }
    Last edited by vwphillips; 10-11-2008 at 11:52 AM.
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •