freeCodeCamp/curriculum/challenges/portuguese/02-javascript-algorithms-an.../basic-javascript/adding-a-default-option-in-...

3.4 KiB

id title challengeType guideUrl videoUrl localeTitle
56533eb9ac21ba0edf2244de Adding a Default Option in Switch Statements 1 https://portuguese.freecodecamp.org/guide/certificates/adding-a-default-option-in-switch-statements Adicionando uma opção padrão em instruções de troca

Description

Em uma instrução switch , você pode não conseguir especificar todos os valores possíveis como instruções case . Em vez disso, você pode adicionar a instrução default que será executada se nenhuma instrução case correspondente for encontrada. Pense nisso como a final else declaração em um if/else cadeia. Uma declaração default deve ser o último caso.
switch (num) {
valor do caso1:
statement1;
pausa;
valor do caso2:
statement2;
pausa;
...
padrão:
defaultStatement;
pausa;
}

Instructions

Escreva uma instrução switch para definir a answer para as seguintes condições:
"a" - "maçã"
"b" - "pássaro"
"c" - "gato"
default - "stuff"

Tests

tests:
  - text: <code>switchOfStuff(&quot;a&quot;)</code> deve ter um valor de &quot;apple&quot;
    testString: 'assert(switchOfStuff("a") === "apple", "<code>switchOfStuff("a")</code> should have a value of "apple"");'
  - text: <code>switchOfStuff(&quot;b&quot;)</code> deve ter um valor de &quot;bird&quot;
    testString: 'assert(switchOfStuff("b") === "bird", "<code>switchOfStuff("b")</code> should have a value of "bird"");'
  - text: <code>switchOfStuff(&quot;c&quot;)</code> deve ter um valor de &quot;cat&quot;
    testString: 'assert(switchOfStuff("c") === "cat", "<code>switchOfStuff("c")</code> should have a value of "cat"");'
  - text: <code>switchOfStuff(&quot;d&quot;)</code> deve ter um valor de &quot;stuff&quot;
    testString: 'assert(switchOfStuff("d") === "stuff", "<code>switchOfStuff("d")</code> should have a value of "stuff"");'
  - text: <code>switchOfStuff(4)</code> deve ter um valor de &quot;stuff&quot;
    testString: 'assert(switchOfStuff(4) === "stuff", "<code>switchOfStuff(4)</code> should have a value of "stuff"");'
  - text: Você não deve usar nenhuma instrução <code>if</code> ou <code>else</code>
    testString: 'assert(!/else/g.test(code) || !/if/g.test(code), "You should not use any <code>if</code> or <code>else</code> statements");'
  - text: Você deve usar uma declaração <code>default</code>
    testString: 'assert(switchOfStuff("string-to-trigger-default-case") === "stuff", "You should use a <code>default</code> statement");'
  - text: Você deve ter pelo menos 3 declarações de <code>break</code>
    testString: 'assert(code.match(/break/g).length > 2, "You should have at least 3 <code>break</code> statements");'

Challenge Seed

function switchOfStuff(val) {
  var answer = "";
  // Only change code below this line



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

// Change this value to test
switchOfStuff(1);

Solution

// solution required