1

I Need to write a for loop like below mention way ie:

var array = [1,2,3,4,5,6,7];

var startday=5
var endday=1

 for(var k=startday;k>endday;k++){

   }

and my output would be like : 5,6,7,1

I have tried the code but the values are coming like 5,6,7,8,9.......

4
  • If you want your loop to stop, you'd need to use something like for (k=startDay;k > endDay; k--) otherwise it loops forever. Commented Feb 11, 2016 at 7:17
  • output ((k - 1) % 7) + 1 Commented Feb 11, 2016 at 7:17
  • @pandu what is your criteria... you need elements starting from your startdate till at the end of your array and the last element should be your end day right? Commented Feb 11, 2016 at 7:22
  • var array = [1,2,3,4,5,6,7]; var startday=5 var endday=1 then the for loop out put is 5,6,7,1 Commented Feb 11, 2016 at 7:27

5 Answers 5

2

Assuming this is to iterate over an array with variable start and end.

  1. Get the start index form the array
  2. Get the end index from the array
  3. Adjust the end if it is smaller than start, add in this case array.length
  4. Iterate over the new start and end indices
  5. Adjust the index, because it can runs outside the array.length with the remainder operator.

var array = [1, 2, 3, 4, 5, 6, 7],
    startday = 5,
    endday = 1,
    k,
    start = array.indexOf(startday),
    end = array.indexOf(endday);

if (end < start) {
    end += array.length;
}
for (var k = start; k <= end; k++ ){
    document.write(array[k % array.length] + '<br>');
}

Sign up to request clarification or add additional context in comments.

Comments

0

You should use --k as third parameter of for (it causes k variable to be decremented)

var array = [1,2,3,4,5,6,7];
var startday=5;
var endday=1
for (var k=startday; k >= endday; --k)
{
}

1 Comment

requested output, 5,6,7,1 is not in descending order
0

inside your loop, print this 'array[k]' , and 'if(k > array.length){ print 1 and break}'

4 Comments

answers a specific case only, what if endday is 2
make a modification print endday instead of 1.
so it would be like this? 'print endday and break' instead of print 1
and there is another issue.. if the array is [4,5,6,7,8,9] and start day is 5 the array[5] will give you 8
0

Does it have to be a for loop? How about this:

var array = [1,2,3,4,5,6,7];
var startday = 5;
var endday = 1;
var k = startday;
while (k > endday){
    // print array[k-1]

    if (k == array.length){
        k = 1;
    } else {
        k++;
    }
}
while (k <= endday){
    // print array[k-1]

    k++;
}

I'm assuming startday and endday refer to an array index.

Comments

0

Try this

<script src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
var array = [11,22,33,44,55,66,77];

var startday=55;
var endday=11;

var lastEl = array[array.length-1];
var index_startday =  $.inArray( startday, array );

 for(var count=index_startday; count <= array.length;count++){
    alert(array[count]);
    if(array[count] == endday){
        break;
    }
    if(array[count] == lastEl){
      count = -1;
      continue;
    }
   }
</script>

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.