【问题标题】:How do I remove the word of the input from an output of objects?如何从对象的输出中删除输入的单词?
【发布时间】:2020-06-12 16:56:28
【问题描述】:

对于措辞不佳的问题,我深表歉意。我真的不知道该问题如何措辞,尤其是因为我是新手。

我正在尝试制作一个小游戏,在其中从 6 种颜色列表中随机分配 4 种颜色,并且要拆除炸弹,您需要按照一些规则切断电线,例如,如果您有蓝线,则不能剪红色,或者如果你有 x 线,你需要剪 y,等等。

我遇到的第一个问题是我不知道如何得到它,所以如果用户输入“绿色”,例如,“绿色”将从 4 种颜色中消失。因此,例如,如果您有黄色、蓝色、绿色和黑色,并且用户选择剪切“绿色”,那么接下来的 3 种颜色将显示“黄色、蓝色和黑色”

谁能帮帮我?

var colors = ['Blue', 'Red', 'Yellow','White', 'Black', 'Green'];
var color = colors[Math.floor(Math.random()*colors.length)];
var color2 = colors[Math.floor(Math.random()*colors.length)];
var color3 = colors[Math.floor(Math.random()*colors.length)];
var color4 = colors[Math.floor(Math.random()*colors.length)];
console.log('The wires are: ' + color + ', ' + color2 + ', ' + color3 + ' and ' + color4);

const asdf = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
});

asdf.question('What will you cut?' , wire => {
    console.log(`You cut ${wire}!`);
    asdf.close();

    if (color == 'Blue' | color2 == 'Blue' | color3 == 'Blue' | color4 == 'Blue' && wire == 'Red') {
        console.log("Wrong wire!");
        return;
    }
    else {
        console.log("Nice!");
        }

    console.log('The remaining wires are: ' );
    asdf.question("Next wire?", wire => {
        console.log(`You cut ${wire}!`);
        asdf.close()
    })
})

【问题讨论】:

  • 您正在使用按位的| 或-您想要逻辑或||
  • 写条件的更优雅的方式是[color, color2, color3, color4].includes('Blue') && wire === 'Red' 而不是color == 'Blue' | color2 == 'Blue' | color3 == 'Blue' | color4 == 'Blue' && wire == 'Red'

标签: javascript


【解决方案1】:

假设用户输入的字符串名为typedColor,则为:

const colors = ['Blue', 'Red', 'Yellow','White', 'Black', 'Green']

const typedColor = 'Green'

const colorWithoutTypedColor = colors.filter(color => color !== typedColor)

console.log(colorWithoutTypedColor)

如果您想将所有内容保持在可变颜色中,还有另一种方法:

const colors = ['Blue', 'Red', 'Yellow','White', 'Black', 'Green']

const typedColor = 'Green'

colors.splice(colors.findIndex(color => color === typedColor), 1)

console.log(colors)

编辑:从 6 个数组中随机选择 4 个颜色

const colors = ['Blue', 'Red', 'Yellow', 'White', 'Black', 'Green']

function getXRandomColors(colors, x, result) {
  if (x === 0) return result // Exit contition (needed in a recursive function)
  const randomIndex = Math.floor(Math.random() * colors.length)
  return getXRandomColors(colors, x - 1, [...result, colors[randomIndex]])
}

fourRandomColors = getXRandomColors(colors, 4, [])

console.log(fourRandomColors)

希望你熟悉递归函数:),否则我可以试着解释一下:

递归函数是调用自身的函数。为了确保您不会进行无限循环(因此,堆栈溢出 ;)),您必须有一个退出条件。 要了解此函数的作用,您可以尝试使用退出条件直接调用它:它返回一个空数组。然后尝试只执行一次(x = 1),你会首先看到,我找到了数组的随机索引。然后,它用同样的colors数组调用自己,x的值少了一个,最后result比以前多了一个值(你可以看到我第一次调用函数result = [] )。

这里,x 就像一个计数器,告诉递归函数何时停止。

这是一个很好的链接:https://www.freecodecamp.org/news/how-recursion-works-explained-with-flowcharts-and-a-video-de61f40cb7f9/

【讨论】:

  • 嘿,这太棒了,但问题是我只想要从数组中随机选择的 4 种颜色,那么用您的解决方案来做到这一点的最佳方法是什么?我尝试为新的 4 种颜色制作另一个数组,但是当我尝试将随机颜色推入其中时,它只是给了我不确定性。非常感谢您的帮助
  • 在随机选择中,你可以选择多次相同的颜色吗?
  • 现在是的,但是一旦我解决了这个问题,我就想改变这一点
  • 啊,这是一个了不起的人,非常感谢你,你帮了大忙。不,我不了解递归函数,如果你能解释它会更加感激,否则我可以试着弄清楚!
  • 我再次编辑了我的答案。提示:如果您设法转换您的数组,以便在调用递归之前删除选择的颜色,您将设法获得您想要的确切行为;)
【解决方案2】:

假设我理解您的问题,使用Set 可能会使事情变得更容易。

您可以使用add 为您的Set 填充4 种随机颜色,或者在构建过程中,然后只要您想删除颜色,只需调用delete

// initialize set through constructor using array of strings
const colorSet = new Set(["Blue", "Green", "Red", "Black"]);
colorSet.delete("Blue"); // remove "Blue" from set

如果您点击有关 Set 的 MDN 链接,您将找到常见操作的示例,例如迭代和日志记录。

【讨论】:

    【解决方案3】:

    您需要将 4 种颜色存储在一个数组中,而不是在变量中。

    const colors = ['Blue', 'Red', 'Yellow','White', 'Black', 'Green'];
    const wires = [
      colors[Math.floor(Math.random()*colors.length)],
      colors[Math.floor(Math.random()*colors.length)],
      colors[Math.floor(Math.random()*colors.length)],
      colors[Math.floor(Math.random()*colors.length)]
    ];
    
    function removeWire(wire) {
      const index = wires.indexOf(wire);
      if (index > -1) {
        wires.splice(index, 1);
      }
    }
    
    // then
    
    removeWire(wire);
    
    

    【讨论】:

    • 不弹出只删除数组中的最后一个元素吗?如果我理解你的话,那是行不通的,不是吗?因为我要删除的颜色可能在数组中的任何位置
    • 是的,对不起。编辑了答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 2014-03-27
    相关资源
    最近更新 更多