Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
How to use If and Boolean to pass a TDD test

Here is the task:

In English, the colors of the rainbow are often listed as:

red orange yellow green blue indigo violet

Declare a function called isRainbowColor. Use case-insensitive checking, i.e. "red", "Red", "RED", "ReD", and "rEd" are all valid rainbow colors.

/**

@param {string} ??? - the color to check @returns {boolean} whether or not the given color is a rainbow color */

Here is what I have so far (I know this is not enough!):

function isRainbowColor(string){
  let rainbowColor = [];
  for (const string of rainbowColor){
    if (string != rainbowColor){
      return true;
}else{
      return false;
    }
  }
}

Here is the error message that I get:

error message

Here is the test that I could not pass with my code above:

actual = isRainbowColor("red");
expected = true;

if (actual === expected) {
  console.log("Test PASSED!");
} else {
  console.error("Test FAILED. Keep trying!");
  console.group("Result:");
  console.log("  actual:", actual);
  console.log("expected:", expected);
  console.groupEnd();
}

actual = isRainbowColor("rEd");
expected = true;

if (actual === expected) {
  console.log("Test PASSED!");
} else {
  console.error("Test FAILED. Keep trying!");
  console.group("Result:");
  console.log("  actual:", actual);
  console.log("expected:", expected);
  console.groupEnd();
}

actual = isRainbowColor("Brown");
expected = false;

if (actual === expected) {
  console.log("Test PASSED!");
} else {
  console.error("Test FAILED. Keep trying!");
  console.group("Result:");
  console.log("  actual:", actual);
  console.log("expected:", expected);
  console.groupEnd();
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here is the function isRainbowColor for your question :

So when using the function, you basically just have to just pass in a string as parameter (it doesn't matter if its uppercase, lowercase or mixed-case) and if it finds that string in the array "rainbowColors" then it will return true and if it doesn't finds that string in rainbowColors array, then it will return false

function isRainbowColor(color) {
    var rainbowColors = ['violet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red'];
    color = color.toString().toLowerCase();

    rainbowColors.forEach(rainbowColor => {
        if (color == rainbowColor) {
            console.log(color + ' is a rainbow color');
            return true;
        }
    });

    return false;
}

Usage

-----------------------------------------------------------------

isRainbowColor('red');
Console : red is a rainbow color
true

-----------------------------------------------------------------

isRainbowColor('hi');
false

-----------------------------------------------------------------

Edit : Added support for all lowercase, uppercase and mixed-case string as the parameter value. Ex :

-----------------------------------------------------------------

isRainbowColor('RED');
red is a rainbow color
true

-----------------------------------------------------------------

isRainbowColor('RedddD');
false

-----------------------------------------------------------------

isRainbowColor('YeLlOW');
yellow is a rainbow color
true

-----------------------------------------------------------------
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda