Facebook
From Flying Gibbon, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 212
  1. <!--
  2.  
  3.  Try to identify and eliminate all bugs in given code
  4.  with minimum number of changes in the code itself.
  5.  
  6.  Describe bugs that do not affect the working of code but it readability (Best practices)
  7.  
  8. -->
  9. <!DOCTYPE html>
  10. <html>
  11. <head>
  12. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  13. <script>
  14. function show_error(message){
  15.         $( "#error" ).html( message );
  16. }
  17.  
  18. function set_result(result){
  19.         $( "#result" ).html( result );
  20. }
  21.  
  22. function isNumericField(id,success){
  23.        
  24.         var x = $("#"+id).val();
  25.         if( $.isNumeric(x)){
  26.                 return success();
  27.         }
  28.         show_error('Field: '+id+' must be a number');
  29.         return false;
  30.        
  31.        
  32. }
  33.  
  34. function isNotZeroField(id,callback){
  35.         var action = 'validate_number_not_zero';
  36.         var x = $("#"+id).val();
  37.         $.ajax({
  38.                   url: "task3API.php",
  39.                   data: {x:x,y:y,action:action},
  40.                   success: function(result){
  41.                           if(result==true)
  42.                                   return callback();
  43.                           else show_error('Field: '+id+' must be a number different then 0');
  44.                   },
  45.                 });    
  46. }
  47.  
  48. $(document).ready(function(){
  49.         action = 'multiply';
  50.     $("#multiply").click(function(){
  51.           var x = $("#x").val();
  52.           var y = $("#y").val();
  53.           var result = isNumericField("x", function(){
  54.                   isNumericField("y", function(){
  55.                           $.ajax({
  56.                                   url: "task3API.php",
  57.                                   data: {x:x,y:y,action:action},
  58.                                   success: function(result){
  59.                                         return result;
  60.                                   }
  61.                                 });
  62.                   })
  63.           });
  64.          set_result( result );
  65.     });
  66.        
  67.         action = 'divide';
  68.         $("#divide").click(function(){
  69.           var x = $("#x").val();
  70.           var y = $("#y").val();
  71.           var result = isNotZeroField("x", function(){
  72.                   isNotZeroField("y",function(){
  73.                           $.ajax({
  74.                                   url: "task3API.php",
  75.                                   data: {x:x,y:y,action:action},
  76.                                   success: function(result){
  77.                                           return result;
  78.                                   },
  79.                                 });
  80.                   })
  81.           });
  82.  
  83.           set_result( result );
  84.         });
  85. });
  86. </script>
  87. </head>
  88. <body>
  89. <h3>Find all the bugs and try to fix them</h3>
  90. <div>
  91.  
  92. <p>Calculate:</p>
  93. <span>x:</span>
  94. <input id='x' value='1'/> <br/>
  95. <span>y:</span>
  96. <input id='y' value='2'/>
  97. <br/> =
  98. <span id='result'></span>
  99. <br/>
  100. <button id="multiply">*</button>
  101. <button id="divide">/</button>
  102. <span id='error'></span>
  103. </div>
  104. </body>
  105. </html>