Jump to content


Photo

P120 Same Code As Book...no Discount Code Shown


  • Please log in to reply
2 replies to this topic

#1 DP76

DP76

    Active Member

  • Members
  • PipPip
  • 43 posts

Posted 31 December 2013 - 09:42 AM

This is the same code as the book. Why does the discount code not show?

$(document).ready(function() {	
			$(".guess_box").click(checkForCode);
			function getRandom(num) {
	var my_num = Math.floor(Math.random()*num);
	return my_num;
	}	
var hideCode = function() {
	var numRand = getRandom(4);
	$(".guess_box").each(function(index, value) {
		if(numRand == index) {
			$(this).append("<span id = 'has_discount'></span>");
			return false;
		}
	});
}
hideCode();

	function checkForCode() {
		var discount;
		if($.contains(this, document.getElementById("has_discount")))
		{
			var my_num = getRandom(100);
			discount = "<p>Your code is "+my_num+"</p>";
		}else{
			iscount="<p>Sorry, no Discount this time!</p>";
		}
			
		$(".guess_box").each(function() {
			if($.contains(this, document.getElementById("has_discount")))
			{
				$(this).addClass("discount");
		} else {
				$(this).addClass("no_discount");
		}
		$(this).unbind();

		}); 
		$("#result").append(discount);
	} // end check for code function
			


$(".guess_box").hover(
	  
	function () {
		
	$(this).addClass("my_hover");
	   
	},
	   
	function () {
		  
	$(this).removeClass("my_hover");
	 
	  });  // End hover
});
 	


Edited by DP76, 31 December 2013 - 09:43 AM.


#2 Stpehen H. W

Stpehen H. W

    New Member

  • Members
  • Pip
  • 5 posts

Posted 20 March 2014 - 09:17 PM

Did you check the .html and .css file? Does element like $('#result') exist? Are there any CSS rules applied to .discount or .no_discount?

#3 Stpehen H. W

Stpehen H. W

    New Member

  • Members
  • Pip
  • 5 posts

Posted 21 March 2014 - 11:26 PM


look at this line:
iscount="<p>Sorry, no Discount this time!</p>";
Shouldn't it be this?
discount="<p>Sorry, no Discount this time!</p>";
The variable name is 'discount', not 'iscount'.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users