Friday 25 May 2018

Solution of Freecodecamp.org problem 162 [Solved]


The number 162th is a tough problem named 'Local Scope and Functions' in Freecodecamp.org.

Here is the problem that they given.


function myLocalScope() {
  'use strict';
  
  
  console.log(myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);

// Now remove the console log line to pass the test



Here is the steps to find the solution and
its video.


 



Steps

  1. Type var myVar = 10; below 'use strict';

  2. 
    function myLocalScope() {
      'use strict';
      var myVar = 10;
      
      console.log(myVar);
    }
    myLocalScope();
    
    // Run and check the console
    // myVar is not defined outside of myLocalScope
    console.log(myVar);
    
    // Now remove the console log line to pass the test
    
    

  3.  Run the code by pressing ctrl+Enter or Clicking Run Button.

  4. 
    function myLocalScope() {
      'use strict';
      var myVar = 10;
      
      console.log(myVar);
    }
    myLocalScope();
    
    // Run and check the console
    // myVar is not defined outside of myLocalScope
    console.log(myVar);
    
    // Now remove the console log line to pass the test
    
    

  5.  Now Remove only the second console.log(myVar);  See below.

  6. 
    function myLocalScope() {
      'use strict';
      var myVar = 10;
      
      console.log(myVar);
    }
    myLocalScope();
    
    // Run and check the console
    // myVar is not defined outside of myLocalScope
    
    
    // Now remove the console log line to pass the test
    
    
    

  7.  Again Run the code by pressing ctrl+Enter or Clicking Run Button. You are done.


The final view of the code will be :

    
    function myLocalScope() {
      'use strict';
      var myVar = 10;
      
      console.log(myVar);
    }
    myLocalScope();
    
    // Run and check the console
    // myVar is not defined outside of myLocalScope
    
    
    // Now remove the console log line to pass the test
    
    
    


If you have any doubts, comment below.

Support me by clicking Thumbs UP :

Subscribe my channel : Tech 2 Life


1 comment: