Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

257
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda