Files

2.0 KiB

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
5690307fddb111c6084545d7 Usar a ordem lógica em instruções if else 1 https://scrimba.com/c/cwNvMUV 18228 logical-order-in-if-else-statements

--description--

A ordem é importante em instruções if e else if.

A função é executada de cima para baixo, então você deve ser cuidadoso com qual instrução vem primeiro.

Tomemos como exemplo estas duas funções.

Aqui está a primeira:

function foo(x) {
  if (x < 1) {
    return "Less than one";
  } else if (x < 2) {
    return "Less than two";
  } else {
    return "Greater than or equal to two";
  }
}

A segunda apenas altera a ordem das instruções if e else if:

function bar(x) {
  if (x < 2) {
    return "Less than two";
  } else if (x < 1) {
    return "Less than one";
  } else {
    return "Greater than or equal to two";
  }
}

Embora as duas funções pareçam praticamente idênticas, se passarmos um número para ambas, teremos saídas diferentes.

foo(0)
bar(0)

foo(0) retornará a string Less than one e bar(0) retornará a string Less than two.

--instructions--

Altere a ordem lógica na função para que retorne as instruções corretas em todos os cenários.

--hints--

orderMyLogic(4) deve retornar a string Less than 5

assert(orderMyLogic(4) === 'Less than 5');

orderMyLogic(6) deve retornar a string Less than 10

assert(orderMyLogic(6) === 'Less than 10');

orderMyLogic(11) deve retornar a string Greater than or equal to 10

assert(orderMyLogic(11) === 'Greater than or equal to 10');

--seed--

--seed-contents--

function orderMyLogic(val) {
  if (val < 10) {
    return "Less than 10";
  } else if (val < 5) {
    return "Less than 5";
  } else {
    return "Greater than or equal to 10";
  }
}

orderMyLogic(7);

--solutions--

function orderMyLogic(val) {
  if(val < 5) {
    return "Less than 5";
  } else if (val < 10) {
    return "Less than 10";
  } else {
    return "Greater than or equal to 10";
  }
}