【问题标题】:How to use If and Boolean to pass a TDD test如何使用 If 和 Boolean 通过 TDD 测试
【发布时间】:2022-01-09 04:13:35
【问题描述】:

这是任务:

在英语中,彩虹的颜色通常被列为:

红橙黄绿蓝靛紫

声明一个名为isRainbowColor 的函数。使用不区分大小写 检查,即"red"、"Red"、"RED"、"ReD"和"rEd"都有效 彩虹色。

/**

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

这是我目前所拥有的(我知道这还不够!):

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

这是我收到的错误消息:

这是我上面的代码无法通过的测试:

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();
}

【问题讨论】:

  • 鉴于您刚刚将其定义为空数组,您希望for (const string of rainbowColor) 做什么?
  • 我知道,我在这个任务中不知所措。

标签: javascript tdd


【解决方案1】:

这里是你的问题的函数 isRainbowColor :

因此,当使用该函数时,您基本上只需传入一个字符串作为参数(不管它是大写、小写还是混合大小写),如果它在数组“rainbowColors”中找到该字符串,那么它将返回 true,如果在 rainbowColors 数组中找不到该字符串,则返回 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;
}

用法

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

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

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

isRainbowColor('hi');
false

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

编辑:添加了对所有小写、大写和混合大小写字符串作为参数值的支持。例如:

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

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

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

isRainbowColor('RedddD');
false

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

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

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

【讨论】:

  • 他们确实定义了那个函数,它在他们问题的第一个代码块中。
  • @jonrsharpe。哦,是的,抱歉,这段代码运行良好
  • 在声明之前使用了哪个变量?
  • @jonrsharpe。当我输入答案时,未定义变量“实际”,然后您编辑了问题并定义了它,但我没有看到并发布了答案。您还可以在错误消息图像中看到,错误显示:结果:实际:未定义预期:真
  • 我只是修正了格式,我没有添加任何代码(尽管它可能使一些不可见的代码可见)。
猜你喜欢
  • 1970-01-01
  • 2011-11-16
  • 2018-10-20
  • 1970-01-01
  • 2017-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多