Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

javascript - 在for循环中调用函数-JavaScirpt(Calling a function within a for loop - JavaScirpt)

Hi I am trying to call a function within a for loop but It isn't working...This is how my code currently looks like:

(嗨,我正在尝试在for循环中调用一个函数,但是它不起作用...这是我的代码当前的样子:)

bot.on('message', data => {
    if (data.type !== 'message' || data.subtype === 'bot_message') {
        return;
    }
    findClassroomMention(data,text);
});

var classrooms = 
    {
        L108: ["lokaal 108","L108","108"],
        L208: ["lokaal 208","L208","208"]
    };

function findClassroomMention(message) {    
    var found = false   
    for(var ClassroomId in classrooms) {
        for(var term of classrooms[ClassroomId]) {
            if(message.includes(term)) {
                found = ClassroomId;
                notifyProblemSolver();
                break;
            }
        }
        if (found) notifyProblemSolver(); break;
    }
    return found
};

function notifyProblemSolver(ClassroomId) {
    const params = {
        icon_emoji: ':smiley:'
    }
    bot.postMessageToChannel('caris','We have a problem in' + ClassroomId, params);
};

I want the function notifyProblemSolver() to be called in the for loop...But if I run the code it isn't working.

(我希望在for循环中调用函数notifyProblemSolver()...但是如果我运行代码,它将无法正常工作。)

Any tips?

(有小费吗?)

Thanks in advance!

(提前致谢!)

  ask by Martijn_- translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think if (found) notifyProblemSolver; break;

(我认为if (found) notifyProblemSolver; break;)

if (found) notifyProblemSolver; break; is the issue.

(是问题。)

That break will be called regardless of if (found) so for(var ClassroomId in classrooms) { will only run once.

(不管if (found) ,都将调用该break ,因此for(var ClassroomId in classrooms) {将仅运行一次。)

I think you meant

(我想你是说)

if (found) { notifyProblemSolver(); break; }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...