1

I have a JavaScript code like so:

var myArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; for (var i = 0, di = 1; i >= 0; i += di) { if (i == myArray.length - 1) { di = -1; } document.writeln(myArray[i]); } 

I need it to stop right in the middle like 10 and from 10 starts counting down to 0 back.

So far, I've managed to make it work from 0 to 20 and from 20 - 0.

How can I stop it in a middle and start it from there back?

Please help anyone!

4
  • So you want to show the first 10 elements in the array then show them in reverse order? Commented Mar 22, 2018 at 17:05
  • store the stopped array in new var Commented Mar 22, 2018 at 17:05
  • 1
    @RaMPrabU could you be a bit more understanding? Maybe post an answer or mark as duplicate? Commented Mar 22, 2018 at 17:07
  • @Ryan Wilson correct Ryen! Commented Mar 22, 2018 at 18:07

6 Answers 6

1

Here is an example using a function which accepts the array and the number of items you want to display forwards and backwards:

var myArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; if(myArray.length === 1){ ShowXElementsForwardsAndBackwards(myArray, 1); } else if(myArray.length === 0) { //Do nothing as there are no elements in array and dividing 0 by 2 would be undefined } else { ShowXElementsForwardsAndBackwards(myArray, (myArray.length / 2)); } function ShowXElementsForwardsAndBackwards(mYarray, numberOfItems){ if (numberOfItems >= mYarray.length) { throw "More Numbers requested than length of array!"; } for(let x = 0; x < numberOfItems; x++){ document.writeln(mYarray[x]); } for(let y = numberOfItems - 1; y >= 0; y--){ document.writeln(mYarray[y]); } } 
Sign up to request clarification or add additional context in comments.

6 Comments

Can't keep the numberOfItems dynamic? since the question asked for the middle of the array, like: ShowXElementsForwardsAndBackwards(myArray, (myArray.length / 2));? Or in the function doesn't keep the numberOfItems parameter and make a var for the middle of the array...
@CassianoMontanari Added your suggestion to answer
@CassianoMontanari added if conditions for array length zero and 1
Thank you for your time and reply! it helps me.
@Melisa no problem, glad I could help. If you are happy with my answer, please mark it as the accepted answer.
|
1

Just divide your array length by 2

var myArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; for (var i = 0, di = 1; i >= 0; i += di) { if (i == ((myArray.length / 2) -1 )) { di = -1; } document.writeln(myArray[i]); }

Comments

0

Could Array.reverse() help you in this matter?

const array = [0,1,3,4,5,6,7,8,9,10,11,12,13,14,15] const getArrayOfAmount = (array, amount) => array.filter((item, index) => index < amount) let arraySection = getArrayOfAmount(array, 10) let reversed = [...arraySection].reverse() console.log(arraySection) console.log(reversed)

And then you can "do stuff" with each array with watever array manipulation you desire.

Comments

0

Couldn’t you just check if you’ve made it halfway and then subtract your current spot from the length?

for(i = 0; i <= myArray.length; i++){ if( Math.round(i/myArray.length) == 1 ){ document.writeln( myArray[ myArray.length - i] ); } else { document.writeln( myArray[i] ); } } 

Unless I’m missing something?

1 Comment

I think that the one of the simplest solution. Thank you.
0

You could move the checking into the condition block of the for loop.

var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; for ( var i = 0, l = (myArray.length >> 1) - 1, di = 1; i === l && (di = -1), i >= 0; i += di ) { document.writeln(myArray[i]); }

3 Comments

The only problem with this is that it is much harder to read, rather than putting it outside and using meaningful variable names
@BotNet, it is just a proof of concept. in reality, two loops would do best.
I like this PoC, just better if the variables are described outside the loop definition :)
0

If you capture the midpoint ( half the length of the array ), just start working your step in the opposite direction.

const N = 20; let myArray = [...Array(N).keys()]; let midpoint = Math.round(myArray.length/2) for ( let i=1, step=1; i; i+=step) { if (i === midpoint) step *= -1 document.writeln(myArray[i]) }

To make things clearer, I've:

  • Started the loop iterator variable (i) at 1; this also meant the array has an unused 0 value at 0 index; in other words, myArray[0]==0 that's never shown
  • Set the the loop terminating condition to i, which means when i==0 the loop will stop because it is falsy
  • Renamed the di to step, which is more consistent with other terminology
  • The midpoint uses a Math.round() to ensure it's the highest integer (midpoint) (e.g., 15/2 == 7.5 but you want it to be 8 )
  • The midpoint is a variable for performance reasons; calculating the midpoint in the loop body is redundant and less efficient since it only needs to be calculated once
  • For practical purpose, made sizing the array dynamic using N
  • Updated to ES6/ES7 -- this is now non-Internet Explorer-friendly [it won't work in IE ;)] primarily due to the use of the spread operator (...) ... but that's easily avoidable

1 Comment

Thank you for your time and the detail explanation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.