简体   繁体   English

while循环中的多个条件不起作用

[英]Multiple conditions in while loop doesn't work

We have an array of objects representing different people in our contacts lists.我们有一组对象,代表我们的联系人列表中的不同人。 We have a lookUpProfile function that takes name as an argument.我们有一个lookUpProfile function,它将名称作为参数。 The function should check if name is an actual contact's firstName. function 应该检查名称是否是实际联系人的名字。 It will print the contact name's on the console, followed by true if the name matches the contact's firstName, otherwise, it will print false.它将在控制台上打印联系人姓名,如果姓名与联系人的名字匹配,则返回 true,否则将打印 false。

I want my while loop to traverse the array until either nameCheck equals true OR i got bigger than contact length (aka it reaches the end of the array).我希望我的 while 循环遍历数组,直到 nameCheck 等于 true 或者我大于接触长度(也就是它到达数组的末尾)。

It seemed like both of the conditions in my while loop (nameCheck == false || i < contacts.length) are not working.似乎我的 while 循环中的两个条件(nameCheck == false || i < contacts.length)都不起作用。 For some reason even when namecheck equals true and i got bigger than contact length, the while loop keeps executing.出于某种原因,即使 namecheck 等于 true 并且我的长度大于接触长度,while 循环也会继续执行。

I know you can achieve the same result with a for loop instead and I had done that.我知道您可以使用 for 循环来实现相同的结果,而我已经做到了。 But for the sake of learning, I would like to know why my while loop doesn't work.但是为了学习,我想知道为什么我的while循环不起作用。

Thank you so so much in advance.非常感谢你。

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");

What the console output:什么控制台output:

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

while loops as long as the condition is true.只要条件为真, while就会循环。 With logical OR ( || ) only one of the conditions needs to be true in order for the loop to continue.使用逻辑 OR ( || ) 时,只需满足其中一个条件,循环才能继续。 You want the logical AND ( && )你想要逻辑与( &&

 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");

EDIT : If you are writing ES6 Code, these are the solutions I would have taken:编辑:如果您正在编写 ES6 代码,这些是我会采取的解决方案:

if you just want to find out if the name is already in the array, the Array.prototype.some function can be used, if you also want to know the index of the found element, you can use Array.prototype.find :如果你只是想知道名字是否已经在数组中,可以使用Array.prototype.some function ,如果你还想知道找到的元素的索引,你可以使用Array.prototype.find

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 undefined if not found
const foundIdx = contacts.find(firstNameMatches)

just adding a little bit of additional info.只是添加一点额外的信息。 Douglas Crockford suggests in his book, “Javascript: The Good Parts”, that it's always better (to avoid ambiguity) to use the identity operator. Douglas Crockford在他的书“Javascript: The Good Parts”中建议,使用身份运算符总是更好(避免歧义)。 In otherwords, it's better to use "===" instead of "=="换句话说,最好使用“===”而不是“==”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM