Javascript 比较循环在整数和整数数组之间不起作用
Javascript Comparison Loop Not Working Between an Integer and an Array of Integers
根据我下面的代码,user_id_org 应该与 orgs.array 中数组的第一个条目匹配。我的这个工作没有问题,但现在无法让它工作,我不确定我改变了什么,也找不到问题所在。当我 运行 下面的代码时,它 return 与 "No! We did not intersection with 1 of multiple orgs!"
应该 return 与 "Yes! We matched with 1 of multiple orgs!"
。我的目标是了解 why/what 我做错了为什么数字不匹配,以及获得修复。
此外,如果有人对我如何改进此代码提出建议,我很乐意听取。
var orgs = [1234,12345,37463,47716,37463];
var org_id = '';
var user_search = 1;
var org_search = 5;
var user_id_org = 1234;
if (org_id == '') {
if (user_search == 1) { //User Known
if (org_search == 1) {
//Check the single org and see if the org ID is tied to the found user, if so attach user to the correct org.
//If no intersection, submit a ticket as the user and do (information to be discussed in the meeting)
var arrayLength = orgs.length;
for (var i = 0; i < arrayLength; i++) {
if (orgs[i] == user_id_org) {
var intersection = 'Yes! We matched with 1 org!'
} else {
var intersection = 'No! We did not intersection with 1 org!'
}
}
console.log(intersection)
} else if (org_search > 1){
//Loop through orgs and see if orgs any intersection the org ID tied to the found user, if so attach user to the correct org.
var arrayLength = orgs.length;
for (var i = 0; i < arrayLength; i++) {
if (orgs[i] == user_id_org) {
var intersection = 'Yes! We matched with 1 of multiple orgs!'
} else {
var intersection = 'No! We did not intersection with 1 of multiple orgs!'
}
}
console.log(intersection)
} else if (org_search == 0) {
//Create a ticket assigned to the user, and flag it (meeting)
console.log('We did find a user, but no orgs at all, off to the dedicated org we go.')
} else {
console.log('Matching Error')
}
} else if (user_search !== 1) {
if (org_search >= 1) {
console.log('We did not find a user but found multiple orgs! To the dedicated org we go.')
} else if (org_search == 1) {
console.log('We did not find a user but found a single org! Assign them to the org.')
} else if (org_search == 0) {
var intersection = 'No intersection because we did not find a user.'
} else {
console.log('No User Found Error')
}
console.log(intersection)
} else {
console.log('Error');
}
} else if (org_id !== '') {
if (user_search == 1) {
var intersection = 'We matched because we had an org ID and found a user.'
} else if (user_search !== 1) {
//Create user and attach them to the org_id.
var intersection = 'We received an org ID but no user, go create them!'
} else {
console.log('Org ID Provided Error')
}
console.log(intersection)
} else {
return 'Primary Org ID Sorting Error';
}
问题在于这些代码行:
for (var i = 0; i < arrayLength; i++) {
if (orgs[i] == user_id_org) {
var intersection = 'Yes! We matched with 1 of multiple orgs!'
} else {
var intersection = 'No! We did not intersection with 1 of multiple orgs!'
}
}
for
循环从 0
继续到 arrayLength - 1
。在第一次迭代中(即 i
是 0
),intersection
变量设置为 'Yes!...'
。在后续迭代中,它被覆盖为 'No!...'
。您应该使用 break
语句提前退出(如果函数中没有更多相关代码,则使用 return
)。
for (var i = 0; i < arrayLength; i++) {
if (orgs[i] == user_id_org) {
var intersection = 'Yes! We matched with 1 of multiple orgs!'
break; // 'skip' the remaining iterations
} else {
var intersection = 'No! We did not intersection with 1 of multiple orgs!'
}
}
// break jumps here
其他建议,
- 学习使用断点或 console.logs 进行调试。
- 使用
===
而不是具有意外类型转换规则的 ==
。
- 使用
let
和const
代替var
- 以分号结束语句
;
- 只是样式,但大多数 JS 避免在单行块周围使用
{}
- 比传统的
for
循环更喜欢 array.forEach
。
根据我下面的代码,user_id_org 应该与 orgs.array 中数组的第一个条目匹配。我的这个工作没有问题,但现在无法让它工作,我不确定我改变了什么,也找不到问题所在。当我 运行 下面的代码时,它 return 与 "No! We did not intersection with 1 of multiple orgs!"
应该 return 与 "Yes! We matched with 1 of multiple orgs!"
。我的目标是了解 why/what 我做错了为什么数字不匹配,以及获得修复。
此外,如果有人对我如何改进此代码提出建议,我很乐意听取。
var orgs = [1234,12345,37463,47716,37463];
var org_id = '';
var user_search = 1;
var org_search = 5;
var user_id_org = 1234;
if (org_id == '') {
if (user_search == 1) { //User Known
if (org_search == 1) {
//Check the single org and see if the org ID is tied to the found user, if so attach user to the correct org.
//If no intersection, submit a ticket as the user and do (information to be discussed in the meeting)
var arrayLength = orgs.length;
for (var i = 0; i < arrayLength; i++) {
if (orgs[i] == user_id_org) {
var intersection = 'Yes! We matched with 1 org!'
} else {
var intersection = 'No! We did not intersection with 1 org!'
}
}
console.log(intersection)
} else if (org_search > 1){
//Loop through orgs and see if orgs any intersection the org ID tied to the found user, if so attach user to the correct org.
var arrayLength = orgs.length;
for (var i = 0; i < arrayLength; i++) {
if (orgs[i] == user_id_org) {
var intersection = 'Yes! We matched with 1 of multiple orgs!'
} else {
var intersection = 'No! We did not intersection with 1 of multiple orgs!'
}
}
console.log(intersection)
} else if (org_search == 0) {
//Create a ticket assigned to the user, and flag it (meeting)
console.log('We did find a user, but no orgs at all, off to the dedicated org we go.')
} else {
console.log('Matching Error')
}
} else if (user_search !== 1) {
if (org_search >= 1) {
console.log('We did not find a user but found multiple orgs! To the dedicated org we go.')
} else if (org_search == 1) {
console.log('We did not find a user but found a single org! Assign them to the org.')
} else if (org_search == 0) {
var intersection = 'No intersection because we did not find a user.'
} else {
console.log('No User Found Error')
}
console.log(intersection)
} else {
console.log('Error');
}
} else if (org_id !== '') {
if (user_search == 1) {
var intersection = 'We matched because we had an org ID and found a user.'
} else if (user_search !== 1) {
//Create user and attach them to the org_id.
var intersection = 'We received an org ID but no user, go create them!'
} else {
console.log('Org ID Provided Error')
}
console.log(intersection)
} else {
return 'Primary Org ID Sorting Error';
}
问题在于这些代码行:
for (var i = 0; i < arrayLength; i++) {
if (orgs[i] == user_id_org) {
var intersection = 'Yes! We matched with 1 of multiple orgs!'
} else {
var intersection = 'No! We did not intersection with 1 of multiple orgs!'
}
}
for
循环从 0
继续到 arrayLength - 1
。在第一次迭代中(即 i
是 0
),intersection
变量设置为 'Yes!...'
。在后续迭代中,它被覆盖为 'No!...'
。您应该使用 break
语句提前退出(如果函数中没有更多相关代码,则使用 return
)。
for (var i = 0; i < arrayLength; i++) {
if (orgs[i] == user_id_org) {
var intersection = 'Yes! We matched with 1 of multiple orgs!'
break; // 'skip' the remaining iterations
} else {
var intersection = 'No! We did not intersection with 1 of multiple orgs!'
}
}
// break jumps here
其他建议,
- 学习使用断点或 console.logs 进行调试。
- 使用
===
而不是具有意外类型转换规则的==
。 - 使用
let
和const
代替var
- 以分号结束语句
;
- 只是样式,但大多数 JS 避免在单行块周围使用
{}
- 比传统的
for
循环更喜欢array.forEach
。