最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

For Loop won't exit in Javascript, looping infinitely - Stack Overflow

programmeradmin2浏览0评论

My script is causing the browser to freeze and asking me to stop the script. Using firebug I can see the for loop is endlessly looping and not making any progress. Here's the loop:

for (var x = 1; x < 7; x++) {
    var y = x; //to stop the value of x being altered in the concat further down
    var questionidd = "mcq_question_id";
    console.log("1 = " + questionidd);
    var questionid = questionidd.concat(y); // mcq_question_id$ctr the question number
    console.log("2 = " + questionid);
    var mcqid = form[questionid].value; // the questions id on db
    console.log("3 = " + mcqid);

    var answerr = "mcq_question";
    var answer = answerr.concat(y); // mcq_question$ctr the questions chosen answer
    var chosenanswer = form[answer].value; // the answers value
    console.log("4 = " + chosenanswer);
    var amp = "&";
    var equal = "=";
    var questionide = questionid.concat(equal); // "mcq_question_id$ctr="
    var questionida = amp.concat(questionide); // "&mcq_question_id$ctr="
    var answere = amp.concat(answer, equal); // "&mcq_question$ctr="
    if (x = 1) {
        send.push(questionide, mcqid, answere, chosenanswer);
    }
    else {
        send.push(questionida, mcqid, answere, chosenanswer);
    }
}

Update - Fixed! Silly mistakes are the worst

My script is causing the browser to freeze and asking me to stop the script. Using firebug I can see the for loop is endlessly looping and not making any progress. Here's the loop:

for (var x = 1; x < 7; x++) {
    var y = x; //to stop the value of x being altered in the concat further down
    var questionidd = "mcq_question_id";
    console.log("1 = " + questionidd);
    var questionid = questionidd.concat(y); // mcq_question_id$ctr the question number
    console.log("2 = " + questionid);
    var mcqid = form[questionid].value; // the questions id on db
    console.log("3 = " + mcqid);

    var answerr = "mcq_question";
    var answer = answerr.concat(y); // mcq_question$ctr the questions chosen answer
    var chosenanswer = form[answer].value; // the answers value
    console.log("4 = " + chosenanswer);
    var amp = "&";
    var equal = "=";
    var questionide = questionid.concat(equal); // "mcq_question_id$ctr="
    var questionida = amp.concat(questionide); // "&mcq_question_id$ctr="
    var answere = amp.concat(answer, equal); // "&mcq_question$ctr="
    if (x = 1) {
        send.push(questionide, mcqid, answere, chosenanswer);
    }
    else {
        send.push(questionida, mcqid, answere, chosenanswer);
    }
}

Update - Fixed! Silly mistakes are the worst

Share Improve this question edited Dec 30, 2012 at 1:03 Omair Vaiyani asked Dec 30, 2012 at 0:57 Omair VaiyaniOmair Vaiyani 5527 silver badges29 bronze badges 1
  • 4 your if (x = 1) should be if (x == 1) – PeterJ Commented Dec 30, 2012 at 0:58
Add a ment  | 

3 Answers 3

Reset to default 6
if (x = 1) {

should be

if (x === 1) {

The === operator pares while the assignment operator = assigns. Many people make this mistake. :)

When the first loop runs, it sets x to zero, and does so infinitely until the process is terminated. That's why the loop doesn't stop.

if (x = 1) { should be if (x === 1) {

Consider switching to an IDE that catches simple programming errors like this.

Looks like you should have "if (x == 1)" instead of "if (x = 1)".

Your code repeatedly sets x to the value 1, rather than checking that it is equivalent to 1.

发布评论

评论列表(0)

  1. 暂无评论