freeCodeCamp/curriculum/challenges/portuguese/02-javascript-algorithms-an.../basic-javascript/local-scope-and-functions.p...

2.4 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
56533eb9ac21ba0edf2244bf Local Scope and Functions 1 Escopo Local e Funções

Description

Variáveis que são declaradas dentro de uma função, assim como os parâmetros da função, possuem escopo local . Isso significa que eles só são visíveis dentro dessa função. Aqui está uma função myTest com uma variável local chamada loc .
function myTest () {
var loc = "foo";
console.log (loc);
}
meu teste(); // registra "foo"
console.log (loc); // loc não está definido
loc não está definido fora da função.

Instructions

Declare uma variável local myVar dentro de myLocalScope . Execute os testes e siga as instruções comentadas no editor. Sugestão
Atualizando a página pode ajudar se você ficar preso.

Tests

tests:
  - text: Nenhuma variável <code>myVar</code> global
    testString: 'assert(typeof myVar === "undefined", "No global <code>myVar</code> variable");'
  - text: Adicione uma variável <code>myVar</code> local
    testString: 'assert(/var\s+myVar/.test(code), "Add a local <code>myVar</code> variable");'

Challenge Seed

function myLocalScope() {
  'use strict'; // you shouldn't need to edit this line

  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

Before Test

var logOutput = "";
var originalConsole = console
function capture() {
  var nativeLog = console.log;
  console.log = function (message) {
    logOutput = message;
    if(nativeLog.apply) {
      nativeLog.apply(originalConsole, arguments);
    } else {
      var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
      nativeLog(nativeMsg);
    }
  };
}

function uncapture() {
  console.log = originalConsole.log;
}

After Test

console.info('after the test');

Solution

// solution required