首页>Program>source

基于下面的代码,user_id_org应该与组织中数组的第一个条目匹配.我可以正常工作,但是现在无法正常工作,不确定我所做的更改,也找不到问题.当我运行下面的代码时,它将返回 "No! We did not intersection with 1 of multiple orgs!"   什么时候应该和 "Yes! We matched with 1 of multiple orgs!"一起返回 .我的目的是要弄明白为什么/为什么我做错了为什么数字不匹配以及得到解决办法。

另外,如果有人对我如何改进此代码有建议,我很想听听。

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';
}
最新回答
  • 1月前
    1 #

    问题在于以下几行代码:

    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   和

      learn to debug your using breakpoints or console.logs.

      use   代替 ===

      以分号 ==结尾的语句

      只是样式,但大多数JS避免使用 let   一线块左右

      更类似 const   到传统的 var   循环。

      ;

  • javascript:仅在检查器上修改HTML之后才渲染SVG折线
  • 如何在Python中检测两个文件是否相同