【问题标题】:Multiple conditions in while loop doesn't workwhile循环中的多个条件不起作用
【发布时间】:2022-01-17 16:56:33
【问题描述】:

我们的联系人列表中有一组代表不同人的对象。 我们有一个以名称为参数的lookUpProfile 函数。 该函数应检查姓名是否为实际联系人的名字。它将在控制台上打印联系人姓名,如果姓名与联系人的名字匹配,则返回 true,否则将打印 false。

我希望我的 while 循环遍历数组,直到 nameCheck 等于 true 或者我大于接触长度(也就是它到达数组的末尾)。

似乎我的 while 循环中的两个条件 (nameCheck == false || i

我知道你可以使用 for 循环来实现相同的结果,而我已经做到了。但是为了学习,我想知道为什么我的while循环不起作用。

提前非常感谢您。

const contacts = [
  {
    firstName: "Akira",
    lastName: "Laine",
    likes: ["Pizza", "Coding", "Brownie Points"],
  },
  {
    firstName: "Harry",
    lastName: "Potter",
    likes: ["Hogwarts", "Magic", "Hagrid"],
  },
  {
    firstName: "Sherlock",
    lastName: "Holmes",
    likes: ["Intriguing Cases", "Violin"],
  },
];

function lookUpProfile(name) {
  var nameCheck = false;
  console.log(nameCheck);
  
  var i= 0;
  console.log (i);

  while (nameCheck == false || i < contacts.length){
    var nameOnContacts = contacts[i].firstName;
    console.log(nameOnContacts);
    nameCheck = nameOnContacts === name;
    console.log(nameCheck);
    i++;
    console.log(i);
  };
 
};

lookUpProfile("Akira");

控制台输出什么:

›
false
›
0
›
Akira
›
true
›
1
›
Harry
›
false
›
2
›
Sherlock
›
false
›
3
›
TypeError: contacts[i] is undefined (/index.js:28)
/index.html

【问题讨论】:

  • 是的,这是预期的行为。使用逻辑 AND 而不是 OR。

标签: javascript arrays while-loop


【解决方案1】:

while 只要条件为真,就会循环。使用逻辑 OR (||) 时,只有其中一个条件为真,循环才能继续。 您需要逻辑与 (&amp;&amp;)

const contacts = [
  {
    firstName: "Akira",
    lastName: "Laine",
    likes: ["Pizza", "Coding", "Brownie Points"],
  },
  {
    firstName: "Harry",
    lastName: "Potter",
    likes: ["Hogwarts", "Magic", "Hagrid"],
  },
  {
    firstName: "Sherlock",
    lastName: "Holmes",
    likes: ["Intriguing Cases", "Violin"],
  },
];

function lookUpProfile(name) {
  var nameCheck = false;
  console.log(nameCheck);
  
  var i= 0;
  console.log (i);

  while (nameCheck == false && i < contacts.length){
    var nameOnContacts = contacts[i].firstName;
    console.log(nameOnContacts);
    nameCheck = nameOnContacts === name;
    console.log(nameCheck);
    i++;
    console.log(i);
  };
 
};

lookUpProfile("Akira");

编辑: 如果您正在编写 ES6 代码,我会采取以下解决方案:

如果你只是想知道名字是否已经在数组中,可以使用Array.prototype.some函数,如果你还想知道找到的元素的索引,你可以使用Array.prototype.findIndex

const name = "Akira";
// function to check a single element
const firstNameMatches = (element) => element.firstName === name;
// is the name in the array at all?
const isInArray = contacts.some(firstNameMatches);
// to get the index of the found element or -1 if not found
const foundIdx = contacts.findIndex(firstNameMatches)

【讨论】:

  • 更正一次,这个表达式contacts.find(firstNameMatches) 将返回对象本身而不是索引。
  • 糟糕,你是对的。更新为使用findIndex(),这就是我的意思。
【解决方案2】:

只是添加一点额外的信息。 Douglas Crockford 在他的书“Javascript: The Good Parts”中建议,使用身份运算符总是更好(避免歧义)。换句话说,最好使用“===”而不是“==”

https://medium.com/@ludico8/identity-vs-equality-battle-of-understanding-vs-758d396e922

【讨论】:

    猜你喜欢
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多