Jump to content


Photo

Cahp 3...am I The Only One


  • Please log in to reply
3 replies to this topic

#1 DP76

DP76

    Active Member

  • Members
  • PipPip
  • 43 posts

Posted 03 January 2014 - 08:28 AM

Hello,
After checking and checking my code..(for a week!) for the last program in Chapter 3 (p120) I could only get the boxes to highlight. I could not get the text too show up (eg"Sorry, no discount this time") I finally admitted defeat and decided to copy and paste the code from the address given on p121. But I get the same result! The text does not show up! If anyone has got this to work could they please post the correct code here. I cannot see what I have done wrong. Surely I can't be the only one who has had this problem?
code below:
$(document).ready(function() {

	$(".guess_box").click( checkForCode );
	
	function getRandom(num){
		var my_num = Math.floor(Math.random()*num);
		return my_num;
	}
	
	var hideCode = function houdini(){
	  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: CODE"+my_num +"</p>";
		}else{
			discount = "<hr>Sorry, no discount this time!" ;
		}
		$(".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 checkForCode function 
	
	$(".guess_box").hover(
	  function () {
		$(this).addClass("my_hover");
	   },
	   function () {
		  $(this).removeClass("my_hover");
	   });  // End hover
});


#2 LongLiveCHIEF

LongLiveCHIEF

    Active Member

  • Members
  • PipPip
  • 20 posts

Posted 03 January 2014 - 09:46 AM

do you have Skype, Google+ hangouts, or another voice chat app? I'm around and at my desk the next 3-4 days and can help you fix this. My book is continually on loan to other's... otherwise I'd post a fix here right now.

Edited by LongLiveCHIEF, 03 January 2014 - 09:54 AM.


#3 lemuel

lemuel

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 577 posts

Posted 03 January 2014 - 06:14 PM

The script you posted works just fine. Are you sure your index.html file is correct?

#4 DP76

DP76

    Active Member

  • Members
  • PipPip
  • 43 posts

Posted 04 January 2014 - 02:36 AM

Lesson learnt. Always check all programs involved. I missed out the <span id="result"></span> in index.html. Can't believe I missed that
thanks very much both for you help : )




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users