Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 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 Answer

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; }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...