1. Alexander Dahl
  2. head-first-jquery

Commits

Alexander Dahl  committed 912d0d4 Draft

make heavy use of functions

  • Participants
  • Parent commits d000642
  • Branches default

Comments (0)

Files changed (1)

File scripts/page87.js

View file
+function getRandom( num )  {
+    return Math.floor( Math.random() * 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;
+        }
+    });
+}
+
 function checkForCode() {
-    var discount = Math.floor((Math.random()*5) + 5);
-    var disc_msg = "<p>Your Discount is " + discount + "%<\/p>";
-    // alert(discount);
+    var disc_msg;
+    if ( $.contains(this, document.getElementById("has_discount") ) ) {
+        var discount = getRandom(20);
+        disc_msg = "<p>Your Discount is " + discount + "%</p>";
+    } else {
+        disc_msg = "<p>Sorry, no discount this time!</p>";
+    }
     $(this).append(disc_msg);
 
     // $(this).unbind("click");
     // });
 }
 
+/*  main    */
 $(document).ready(function(){
     $(".guess_box").click( checkForCode );
-}); // \ready
+    hideCode();
+});