根据我下面的代码,user_id_org 应与 orgs 中数组的第一个条目匹配。我本来可以正常工作,但现在无法让它工作,不确定我更改了什么,也找不到问题。当我运行下面的代码时,它返回 “不!我们没有与多个组织中的 1 个相交!”
,而它应该返回 “是!我们与多个组织中的 1 个匹配!” “
。我的目标是了解为什么/我做错了什么以及为什么数字不匹配,并进行修复。
此外,如果有人对我如何改进此代码有建议,我很乐意听到。
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.log 进行调试。
- 使用
===
而不是==
,后者具有意外的类型转换规则。 - 使用
let
和const
代替var
- 以分号结束语句
;
- 只是样式,但大多数 JS 避免在单行 block 周围使用
{}
- 更喜欢
array.forEach
而不是传统的for
循环。
关于Javascript 比较循环在整数和整数数组之间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60100921/