while 循环中的多个条件不起作用
Multiple conditions in while loop doesn't work
我们有一组对象代表我们的联系人列表中的不同人。
我们有一个以名称作为参数的 lookUpProfile 函数。
该函数应检查姓名是否为实际联系人的名字。它将在控制台上打印联系人姓名,如果该姓名与联系人的名字匹配,则后跟 true,否则将打印 false。
我希望我的 while 循环遍历数组,直到 nameCheck 等于 true 或我的长度大于接触长度(也就是它到达数组的末尾)。
我的 while 循环中的两个条件 (nameCheck == false || i < contacts.length) 似乎都不起作用。
出于某种原因,即使 namecheck 等于 true 并且我的长度大于接触长度,while 循环仍会继续执行。
我知道您可以使用 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
while
循环,只要条件为真。使用逻辑或 (||
) 时,只有其中一个条件为真才能使循环继续。
你想要逻辑 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");
编辑:
如果您正在编写 ES6 代码,这些是我会采取的解决方案:
如果你只是想知道这个名字是否已经在数组中,Array.prototype.some
function can be used, if you also want to know the index of the found element, you can use 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)
只是添加了一些额外的信息。 Douglas Crockford 在他的书“Javascript: The Good Parts”中建议,使用身份运算符总是更好(以避免歧义)。换句话说,最好使用“===”而不是“==”
https://medium.com/@ludico8/identity-vs-equality-battle-of-understanding-vs-758d396e922
我们有一组对象代表我们的联系人列表中的不同人。 我们有一个以名称作为参数的 lookUpProfile 函数。 该函数应检查姓名是否为实际联系人的名字。它将在控制台上打印联系人姓名,如果该姓名与联系人的名字匹配,则后跟 true,否则将打印 false。
我希望我的 while 循环遍历数组,直到 nameCheck 等于 true 或我的长度大于接触长度(也就是它到达数组的末尾)。
我的 while 循环中的两个条件 (nameCheck == false || i < contacts.length) 似乎都不起作用。 出于某种原因,即使 namecheck 等于 true 并且我的长度大于接触长度,while 循环仍会继续执行。
我知道您可以使用 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
while
循环,只要条件为真。使用逻辑或 (||
) 时,只有其中一个条件为真才能使循环继续。
你想要逻辑 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");
编辑: 如果您正在编写 ES6 代码,这些是我会采取的解决方案:
如果你只是想知道这个名字是否已经在数组中,Array.prototype.some
function can be used, if you also want to know the index of the found element, you can use 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)
只是添加了一些额外的信息。 Douglas Crockford 在他的书“Javascript: The Good Parts”中建议,使用身份运算符总是更好(以避免歧义)。换句话说,最好使用“===”而不是“==”
https://medium.com/@ludico8/identity-vs-equality-battle-of-understanding-vs-758d396e922