freeCodeCamp/curriculum/challenges/portuguese/02-javascript-algorithms-an.../basic-javascript/global-vs.-local-scope-in-f...

1.8 KiB

id title challengeType videoUrl localeTitle
56533eb9ac21ba0edf2244c0 Global vs. Local Scope in Functions 1 Escopo global vs. local em funções

Description

É possível ter variáveis locais e globais com o mesmo nome. Quando você faz isso, a variável local tem precedência sobre a variável global . Neste exemplo:
var someVar = "Chapéu";
function myFun () {
var someVar = "Head";
return someVar;
}
A função myFun retornará "Head" porque a versão local da variável está presente.

Instructions

Adicione uma variável local à função myOutfit para substituir o valor de outerWear por "sweater" .

Tests

tests:
  - text: Não altere o valor do <code>outerWear</code> global
    testString: 'assert(outerWear === "T-Shirt", "Do not change the value of the global <code>outerWear</code>");'
  - text: <code>myOutfit</code> deve retornar <code>&quot;sweater&quot;</code>
    testString: 'assert(myOutfit() === "sweater", "<code>myOutfit</code> should return <code>"sweater"</code>");'
  - text: Não altere a declaração de retorno
    testString: 'assert(/return outerWear/.test(code), "Do not change the return statement");'

Challenge Seed

// Setup
var outerWear = "T-Shirt";

function myOutfit() {
  // Only change code below this line



  // Only change code above this line
  return outerWear;
}

myOutfit();

Solution

// solution required