JavaScript Examples

Based on what we have studied and learned this semester, you should be able to simulate the execution of JavaScript programs with complexity like these. Make sure you can show your work using the "memory map" method we have used in class.


  1. What is printed in the "alert" box at the end of running this JavaScript code fragment (assume myMain is executed first)?
    var glob = 10;
    
    function myMain () {
      var glob = 5;
      var k = 0;
      k = foo(12);
      alert(k);
      return ;
    }
    
    function foo (x) {
      var glob = 8;
      x = x + glob;
      return x;
    }
    
    
    answer: ________________ 
    
    
  2. What is printed in the "alert" box at the end of running this JavaScript code fragment (assume myMain is executed first)?
    var glob = 10;
    
    function myMain () {
      var glob = 5;
      var k = 0;
      k = foo(12);
      alert(k);
      return ;
    }
    
    function foo (x) {
      x = x + glob;
      return x;
    }
    
    answer: ________________ 
    
    
    
  3. What is printed in the "alert" box at the end of running this JavaScript code fragment (assume the function myMain runs first)?
       
    function myMain () {
      var ob = makeNewObj();
      ob.linc(); 
      ob.linc(); 
      alert(ob.lrep());
      return;
    }
    
    function makeNewObj() {
      var loc_k = 0;
        
      var obj = {  n:"spring", x:15, flag:true, loc_f:12,
        linc: function() { loc_k++; } ,
        lrep: function() { return loc_k; }
      };
    
      obj.linc(); 
      return obj; 
    }
    
    
    answer: ________________ 
    
    
  4. What is printed in the "alert" box at the end of running this JavaScript code fragment (assume the myMain function runs first)?
       
    function myMain () {
      var ob = makeNewObj();
      ob.finc();
      alert(ob.loc_f + ", " + ob.loc_k);
      return;
    }
    
    function makeNewObj() {
      var loc_k = 0;
    
      var obj = { n:"spring", x:15, flag:true, loc_f:12,
        linc: function() { loc_k++; } ,
        lrep: function() { return loc_k; } ,
        finc: function() { this.loc_f++; }
      };
    
      return obj; 
    }
    
    
    
    answer: ________________ 
    
    
  5. What is printed in the "alert" box at the end of running this JavaScript code fragment?
       
      var tag = 8;
      var val = 4;
      
      tag = tag-val;
      switch (tag) {
        case 0: val = val * 2; break;
        case 1: val = val / 3; break;
        case 2: val = val + 4; break;
        case 3: val = 5; break;
        case 4: val = val * tag; break;
        case 5: val = val - 1; break;
      }
      alert( val );
    
    
    answer: ________________ 
    
    
  6. What is printed in the "alert" box at the end of running this JavaScript code fragment?
       
      var sum = 0;
      var k = 1;
      var done = false;
      
    
      while (!done) {
        sum = sum + k;
        k += 3;
        if (k > 15) { done = true; }
      }
      alert(sum);
    
    
    answer: ________________ 
    
    
  7. What is printed in the "alert" box at the end of running function "myMain"?
       
    function myMain() {
      var func;
      var n = 2;
      var fns = [ foo, bar ];
      alert(fns[0](fns[1](n)));
      return;
    }
    
    function foo (n) { var x = n*n; return x - n; }
    
    function bar (n) { var x = n+1; return x * n; }
       
    
    
    answer: ________________ 
    
    
  8. What is printed in the "alert" box at the end of running function "myMain()" ?
       function myMain() {
          var max = 8;
          var tot = 15;
          tot = 3 * mash ( max-2 );
          alert("tot: " + tot + ", max: " + max);
       }
    
       function mash ( tot ) {
          var tweak = 3 ;
          var res = 10 ;
          res = (tot + tweak) * res ;
          tot = 11;
          return res;
       }
    
    
    answer: ________________ 
    
    
  9. What is printed in the "alert" box at the end of running function "myMain()" ?
       function myMain() {
          var max = 8;
          var num = 15;
          max = 3 * mash ( max-2 );
          alert("num: " + num + ", max: " + max);
       }
    
       function mash ( num ) {
          var res = 10 ;
          res = (num * res) + tweak(res) ;
          num = 1;
          return res;
       }
    
       function tweak ( num ) {
          var mash = 4;
          return num + mash;
       }
    
    answer: ________________ 
    
    
  10. What is printed in the "alert" box at the end of running function "myMain()" ?
       function myMain() {
         var zoom = 5;
         alert(mash(tweak(zoom)));
       }
    
       function mash ( num ) {
         var zoom = 4;
         return tweak(num)+zoom;
       }
    
       function tweak ( num ) {
         var zoom = 3;
         return zoom*num;
       }
    
    answer: ________________ 
    
    
  11. What is printed in the "alert" box at the end of running function "myMain()" ?
       function myMain() {
         var fun;
         fun = mash(5);
         alert(fun);
       }
    
       function mash ( num ) {
         if (num==1) { return 1; }
         else {
           return num * mash(num-1);
         }
       }
    
    answer: ________________ 
    
    
  12. What is printed in the "alert" box at the end of running function "myMain()" ?
       function myMain() {
         var fun = 0;
         fun = mash(32);
         alert(fun);
       }
    
       function mash ( num ) {
         var res;
         if (num==2) { return 2; }
         else {
           res = 2 + mash(tweak(num));
           return res;
         }
       }
    
       function tweak ( arg ) { return arg/2; }
    
    answer: ________________