1

I have a for loop within some Javascript code and can't seem to figure out why it is not executing. I have some console.log statements where I am trying to see whether the variables are capturing what I need them to. However, when I run the code, I do not see any output from my console.log commands. Is there something I am missing here? Please see the Javascript code below:

var strArry = [];  

for(var i=0; i<obj[0].srcLanguageSentence.text; i++)
{

    // create variables representing substrings of the Source language Sentence
    var s1 = text.substring((obj[i].srcLanguageSentence.roles[i].beginOffset - obj[i].srcLanguageSentence.roles[i].beginOffset),(obj[i].srcLanguageSentence.roles[i].beginOffset - 1));
    var s2 = text.substring(obj[i].srcLanguageSentence.roles[i].beginOffset,obj[i].srcLanguageSentence.roles[i].endOffset);
    var s3 = text.substring(obj[i].srcLanguageSentence.roles[i].endOffset,obj[i].srcLanguageSentence.text.length);

    strArry.push(s1)
    strArry.push(s2)

    if(i == obj[0].srcLanguageSentence.roles.length)
    {
        strArry.push(s3);
    }

    text =  s3;

 console.log("s1: " + s1);
 console.log("s2: " + s2);
 console.log("s3: " + s3);
 console.log(s1+s2+s3);
}
1
  • 1
    should be for(var i=0; i<obj[0].srcLanguageSentence.length; i++) Commented Jul 8, 2015 at 6:39

3 Answers 3

1

Your problem is your conditional statement in the for loop:

i < obj[0].srcLanguageSentence.text;

You say that this loop should run for as long as i is less than obj[0].srcLanguageSentence.text, which looks wrong.

I guess you either want to run the for loop for every object in obj (as you take obj[i] in your code)? If so, write it like:

for (var i=0; i < obj.length; i++)

or, if it is for the length of the text:

for (var i=0; i < obj[0].srcLanguageSentence.text.length; i++)
Sign up to request clarification or add additional context in comments.

Comments

1

With the incomplete snippet, there is hardly any detailed suggestion, though it looks like the issue is with your conditional statement. The general approach you may want to try, not only for this particular case, but for others as well, is adding more console.log to identify the issue, assuming u do not want to use the chrome debugger. For example,

var strArry = [];  

console.log(obj[0].srcLanguageSentence.text);
for(var i=0; i<obj[0].srcLanguageSentence.text; i++)
{
   console.log("enter:"+i);
    // create variables representing substrings of the Source language Sentence
    var s1 = text.substring((obj[i].srcLanguageSentence.roles[i].beginOffset - obj[i].srcLanguageSentence.roles[i].beginOffset),(obj[i].srcLanguageSentence.roles[i].beginOffset - 1));
  console.log('s1:'+s1);
    var s2 = text.substring(obj[i].srcLanguageSentence.roles[i].beginOffset,obj[i].srcLanguageSentence.roles[i].endOffset);
    var s3 = text.substring(obj[i].srcLanguageSentence.roles[i].endOffset,obj[i].srcLanguageSentence.text.length);

    strArry.push(s1)
    strArry.push(s2)

    if(i == obj[0].srcLanguageSentence.roles.length)
    {
        strArry.push(s3);
    }

    text =  s3;

 console.log("s1: " + s1);
 console.log("s2: " + s2);
 console.log("s3: " + s3);
 console.log(s1+s2+s3);
}

hope it helps.

Comments

0

Your loop is getting unsuccessful just because of your statement

for(var i=0; i<obj[0].srcLanguageSentence.text; i++)

Here in the for loop you need to put a condition in the numeric form and not in the string or text form.. Hence replace the condition in the looping statement and definitely you will get success in your loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.