首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何告诉Javascript将第一个元素与第二个元素以及第二个元素与第三个元素进行比较,等等。

如何告诉Javascript将第一个元素与第二个元素以及第二个元素与第三个元素进行比较,等等。
EN

Stack Overflow用户
提问于 2022-10-12 17:01:07
回答 4查看 60关注 0票数 -3

大家好,我正在用Javascript来解决这个问题。

我想对数组中的每个元素“自动化”我的代码。我试图找出如何循环到该数组中,并将元素n1与元素n2等进行比较。这是我的一种伪代码:

是0位的动物===“羊”吗?如果不回“嘿,狼走开!”但如果是的话,我想开始另一个比较,动物的位置是1 ===到动物在0位置吗?

如果是的话,还说“嘿,羊,n0:你好吗?”如果不回‘嘿,羊n0:你要被吃掉!;动物在第二个位置的位置===是动物吗?如果是,’羊n1:你好吗?‘如果不回“羊n1”,你就要被吃掉了!诸若此类

我把我的伪代码设置成这样:

代码语言:javascript
复制
 function warnTheSheep(animal) {
  if (animal[0] === "wolf") {
    return "hey wolf go away!";
  } else if (animal[0] === "sheep" && animal[1] === "wolf") {
    return `hey sheep n(i) you are going to be eaten!! run`;
  } else {
    return "hey sheep n(i) hello how are you?";
  }
}

console.log(warnTheSheep(["sheep", "wolf", "wolf", "sheep"]));

谢谢您一直鼓励我。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2022-10-12 17:54:29

根据我从您的问题中收集到的信息,一种非常简单的递归方法可能如下所示:

代码语言:javascript
复制
function warnTheSheep(animal, index = 0) {
    if (index >= animal.length) {
        return;
    }

    if (animal[index] === "wolf") {
        console.log(`hey wolf n(${index}) go away!`);
    } else if (animal[index] === "sheep" && animal[index + 1] === "wolf") {
        console.log(`hey sheep n(${index}) you are going to be eaten!! run`);
    } else {
        console.log(`hey sheep n(${index}) hello how are you?`);
    }

    warnTheSheep(animal, index + 1);
}

warnTheSheep(["sheep", "wolf", "wolf", "sheep"]);

你的问题很不清楚,希望以后能作出一些澄清。

票数 2
EN

Stack Overflow用户

发布于 2022-10-12 17:53:04

这就是我将如何生成一组有用的警告消息。

getWarnings函数为动物数组中的每个插槽生成一个状态数组。warnTheSheep函数只是将其转换为另一个良好的人类可读的消息数组。

代码语言:javascript
复制
const Status = {
  Danger: 0,
  Safe: 1,
  Wolf: 2
}

const Animal = {
  Wolf: "wolf",
  Sheep: "sheep"
}

function getWarnings(animals) {
  const result = [];
  for (let i = 0; i < animals.length; i++) {
    const animal = animals[i];
    const nextAnimal = animals[i + 1];
    if (animal === Animal.Wolf)
      result.push(Status.Wolf);
    else if (animal === Animal.Sheep && nextAnimal === Animal.Wolf)
      result.push(Status.Danger);
    else
      result.push(Status.Safe);
  }
  return result;
}

function warnTheSheep(animals) {
  const result = [];
  const warnings = getWarnings(animals);
  for (let i = 0; i < warnings.length; i++) {
    const warning = warnings[i];
    if (warning === Status.Safe)
      result.push(`hey sheep ${i} hello how are you?`);
    else if (warning === Status.Danger)
      result.push(`hey sheep ${i} you are going to be eaten!! run`);
    else
      result.push(`hey wolf go away`);
  }
  return result;
}

console.log(warnTheSheep(["sheep", "wolf", "wolf", "sheep"]));

票数 0
EN

Stack Overflow用户

发布于 2022-10-12 23:34:57

我会考虑为给定的动物列表创建一个威胁矩阵。然后你可以过滤对动物的威胁。

注意:这不处理动物的威胁,没有动物,如通过["goat"],但我会留给你们解决。

代码语言:javascript
复制
var animals = ["sheep", "wolf", "wolf", "sheep", "sheep"];
var animalthreats = [{
    name: "wolf",
    actions: [{
        targ: "sheep",
        do: "eat yummy"
      },
      {
        targ: "wolf",
        do: "say bye"
      }
    ]
  },
  {
    name: "sheep",
    actions: [{
        targ: "sheep",
        do: "Hi friend"
      },
      {
        targ: "wolf",
        do: "run from"
      }
    ]
  }
];

function warnTheSheep(beasts) {
  let x = "";
  beasts.forEach(function(beast, beastIndex) {
    x = beast;
    console.log("\n--------------------\nBeast:", x, beastIndex, "\n--------------------\n");
    let t = beasts[beastIndex + 1];
    if (typeof t == "undefined") return;
    let act = animalthreats.filter(threat => threat.name == t)[0];
    if(typeof act =="undefined") return;
    var dothis = act.actions.filter(a => a.targ == x)[0];
    console.log("I am", t, "I", dothis.do, x);
  });
  return "Done! no threat in line for " + x;
}

console.log(warnTheSheep(animals));
console.log(warnTheSheep(["sheep"]));
console.log(warnTheSheep(["pig","goat"]));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74045553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档